2017-09-27 24 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