2017-09-27 72 views
0

我有我的自定义异常类从boost::property_tree::ptree_bad_data推导如下:没有匹配函数调用“的boost :: property_tree :: ptree_bad_data :: ptree_bad_data()

class MyCustomException : public boost::property_tree::ptree_bad_data 
{ 
    public: 
     explicit MyCustomException(const std::string& msg): mMsg(msg) {} 
     virtual ~MyCustomException() throw() {} 
     virtual const char* what() const throw() { return mMsg.c_str(); } 

    private: 
     std::string mMsg; 
}; 

在编译过程中我得到的错误是:

error: no matching function for call to ‘boost::property_tree::ptree_bad_data::ptree_bad_data()’ explicit MyCustomException(const std::string& msg): mMsg(msg) {} ^ note: candidate expects 2 arguments, 0 provided explicit MyCustomException(const std::string& msg): mMsg(msg) {} ^

任何想法可能是什么原因?

+1

您显示的代码和错误消息不匹配。发布有关构建错误的问题时,请使用您向我们展示的[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)中的错误。 –

+0

@Someprogrammerdude我编辑了错误。 – astre

+2

ptree_bad_data没有默认ctor,因此是错误。初始化您的基地 –

回答

2

根据文档,类ptree_bad_data没有无参数的构造函数。它实际上是一个构造函数:

template<typename T> ptree_bad_data(const std::string &, const T &); 

所以,你必须提供在构造函数这两种说法:

explicit MyCustomException(const std::string& msg) 
    : boost::property_tree::ptree_bad_data(msg, nullptr /* correct data here */) 

也没有必要单独的异常类存储异常信息。标准异常类将为您做到这一点。

顺便说一句,你确定你想从ptree_bad_data得到你的例外吗?

+0

谢谢。有效。原因是使用更具体的异常而不是std :: exception。任何特殊原因,这可能不是个好主意? – astre

+1

@astre然后从std :: exception派生,如果您没有从'ptree_bad_data'派生的特定原因(如果不使用其所有构造函数参数,则可能没有这个派生)。 – vasek

相关问题