简体   繁体   中英

no matching function for call to ‘boost::property_tree::ptree_bad_data::ptree_bad_data()

I have my custom exception class derived from boost::property_tree::ptree_bad_data as follows:

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;
};

During compilation I get errors as:

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) {} ^

Any idea what could be the reason?

According to the documentation the class ptree_bad_data doesn't have parameterless constructor. It has actually single constructor:

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

So you have to provide these two arguments in your constructor:

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

There is also no need for your exception class to store exception message separately. Standard exception classes will do that for you.

Btw, are you sure you want to derive your exception from ptree_bad_data ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM