0

文字列を解析し、文字列内のデータに基づいてオブジェクトを返す C++11 クラスを作成しました。返したいオブジェクトは次のように定義されています。

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

オブジェクトを返す関数は次のように定義されます。

// 文字列を解析し、コンポーネントに分割します

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

私は(私が思うに)この方法で関数を呼び出せるようにしたいと思っています:

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

しかし、コンパイラは次のように不平を言います:

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

お分かりのように、私はテンプレートの専門家ではありません。私がやろうとしていることは、テンプレートを使用する承認された方法ですか、それとも他の方法を好むべきですか?

4

1 に答える 1

4

Topic<vector<string>>ここでは、テンプレート引数として使用しても意味がありません。削除するだけTopicです:

auto a = TopicParser::parse<vector<string>>(message);
auto b = TopicParser::parse<string>(message);
于 2013-05-19T05:20:05.017 に答える