NotImplementedException C ++

时间:2017-03-21 22:23:48

标签: c++ move

从答案here我实施了我的课程NotImplementedException

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

在另一个类中,我想抛出以下异常(相同的命名空间):

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

to_string方法是抽象基类的重写方法。

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

不幸的是,前面代码的编译显示了这个错误:

error C2280: BSE::NotImplementedException::NotImplementedException(void)': attempting to reference a deleted function`

here我理解它与移动构造函数或赋值有关的问题根据cppreference.com - throw (1)可能就是这种情况:

  

首先,从表达式复制初始化异常对象(这可以调用rvalue表达式的移动构造函数,复制/移动可能需要复制省略)

我尝试添加

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

到我的班级,但那给了我

error C2512: 'BSE::NotImplementedException': no appropriate default constructor available

据我所知std::logic_error没有定义默认构造函数。

:我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

应该是这样的:

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

然后

std::string to_string() override
{
    throw NotImplementedException(); // with new.
}
相关问题