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)可能期望它的第一个参数与它的第二个模板参数的类型相同。

换句话说; TinmRootElement->attrib<T>(key, defaultValue)必须与defaultValue.

于 2010-05-27T21:57:55.770 回答