Philipp Philipp - 1 year ago 420
C++ Question

NotImplementedExcpetion C++

From the answer here I implemented my

NotImplementedException


//exceptions.h
namespace base
{
class NotImplementedException : public std::logic_error
{
public:
virtual char const* what() { return "Function not yet implemented."; }
};
}


In another class I want to throw this exception (same namespace)

std::string to_string() override
{
throw new NotImplementedException();
}


The
to_string
method is an overriden method from an abstract base class.

namespace BSE {
class BaseObject
{
virtual std::string to_string() = 0;
};
}


This gives me the error
error C2280: 'BSE::NotImplementedException::NotImplementedException(void)': attempting to reference a deleted function


From here I understood that it has something to do with move constructor or assignment which according to cppreference.com - throw (1) this could be the case:


First, copy-initializes the exception object from expression (this may call the move constructor for rvalue expression, and the copy/move may be subject to copy elision)


I tried adding

NotImplementedException(const NotImplementedException&) = default;
NotImplementedException& operator=(const NotImplementedException&) = default;


to my class but that gives me
error C2512: 'BSE::NotImplementedException': no appropriate default constructor available
and as far as I know
std::logic_error
has no default constructor defined. How do I get around this?

Answer Source

It should be something like:

namespace base
{
    class NotImplementedException : public std::logic_error
    {
    public:
        NotImplementedException () : std::logic_error{"Function not yet implemented."} {}
    };
}

And then

std::string to_string() override
{
    throw NotImplementedException(); // with new.
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download