NotImplementedException C ++

问题描述 投票:0回答:1

从答案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没有定义默认构造函数。

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

c++ move
1个回答
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.
}
© www.soinside.com 2019 - 2024. All rights reserved.