2017-09-27 2 views
0
다음과 같이 내 사용자 정의 예외 클래스는 boost::property_tree::ptree_bad_data에서 파생 한

:부스트 '에 대한 호출에 대한 일치 기능 없음 :: 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

표시되는 코드와 오류 메시지가 일치하지 않습니다. 빌드 오류에 대한 질문을 게시 할 때 우리에게 표시되는 [Minimal, Complete, Verifiable Example] (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 */) 

별도로 저장 예외 메시지로 예외 클래스에 대한 필요도 없다. 표준 예외 수업이 당신을 위해 그것을 할 것입니다.

Btw, ptree_bad_data에서 예외를 파생 하시겠습니까?

+0

감사. 그것은 효과가 있었다. 그 이유는 std :: exception 대신보다 구체적인 예외를 사용하기 위해서였습니다. 특별한 이유가 있다면 좋지 않을까요? – astre

+1

@astre는'ptree_bad_data'에서 파생되는 특별한 이유가 없다면 std :: exception에서 파생됩니다 (모든 생성자 매개 변수를 사용하지 않을 경우). – vasek