2013-05-19 2 views
0

문자열을 구문 분석하고 문자열의 데이터를 기반으로 개체를 반환 할 C++ 11 클래스를 만들었습니다. 내가 반환 할 목적은 다음과 같이 정의된다함수에서 다른 템플릿 특수화를 반환

// 문자열을 구문 분석 I (구성 요소로

class TopicParser 
{ 
public: 
    template <class T> 
    static Topic<T> 
    parse(string message) 
    { 
    T data; // string, vector<string> or map<string, string> 
    string id = "123"; 
    Topic<T> t(id, data); 
    return t; 
    } 
}; 

그것을 분할 :

// Container for the topic data and id 
template <typename T> 
class Topic 
{ 
public: 
    Topic(string id, T data) 
    : _id(id), _data(data) 
    {} 

private: 
    string _id; 
    T _data; 
}; 

객체를 반환하는 함수는 다음과 같이 정의된다 나는이 방법으로 함수를 호출 할 수 있기를 원한다고 생각한다 :

string message = "some message to parse..."; 
auto a = TopicParser::parse<Topic<vector<string>>>(message); 
auto b = TopicParser::parse<Topic<string>>(message); 

컴파일러 compla 그 이유는 다음과 같습니다.

no matching function for call to ‘Topic<std::vector<std::basic_string<char> > >::Topic()’ 

나는 템플릿 전문가가 아닙니다. 내가 다른 방법을 선호해야하는 템플릿을 사용하는 승인 된 방법을 시도하고 있습니까?

+0

을, 나는 컴파일러는 각각 당신이 어딘가에 코드에서 사용하고자하는 모든 전문을 정의 할 수에서 기대하는 것이라 생각합니다. – didierc

+1

''을'parse '에 템플릿 인수로 전달하고 있습니다. 'Topic '을 반환합니다. 'Topic <주제어 >'을 되 찾으시겠습니까? –

답변

4

템플릿 인수로 Topic<vector<string>>을 사용하면 여기서는 쓸모가 없습니다. 그냥 Topic을 제거 : 정적과`parse` 방법을 선언하고

auto a = TopicParser::parse<vector<string>>(message); 
auto b = TopicParser::parse<string>(message); 
+0

예, 저는 T를 정의하기 위해 정의한 것을 오해했습니다. 고맙습니다! –

관련 문제