0

TinyXML (c++) を使用して xml ファイルからデータを読み込もうとしています。

int height = rootElem->attrib<int>("height", 480);

rootElemは、読み込まれた xml ファイルのルート要素です。そこから高さの値をロードしたい(整数)。しかし、私はこのようなもののためのラッパー関数を持っています:

template<typename T>
T getValue(const string &key, const string &defaultValue = "")
{
    return mRootElement->attrib<T>(key, defaultValue);
}

文字列で動作します:

std::string temp = getValue<std::string>("width");

そして、フェッチ中に失敗します:

int temp = getValue<int>("width");


>no matching function for call to ‘TiXmlElement::attrib(const std::string&, const std::string&)’

UPD : 新しいバージョンのコード:

template<typename T>
T getValue(const string &key, const T &defaultValue = T())
{
    return mRootElement->attrib<T>(key, defaultValue);
}
4

2 に答える 2

1

その理由は、TiXmlElement :: attribのintバージョンを呼び出しているが、const std :: string&型のdefualtValueを指定しているためです。ただし、関数はint型のdefaultValueを想定しています。

于 2010-05-27T21:57:52.573 に答える
1

attrib<T>(key, defaultValue)おそらく、最初の引数が2番目のテンプレート引数と同じタイプであることを期待します。

言い換えると; Tinは。mRootElement->attrib<T>(key, defaultValue)と同じタイプである必要がありますdefaultValue

于 2010-05-27T21:57:55.770 に答える