我的自定义异常类派生自数据,如下所示:
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) {} ^
知道原因是什么吗?
发布于 2017-09-27 08:55:56
根据文档,类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派生异常吗?
https://stackoverflow.com/questions/46442777
复制相似问题