我试图用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);
}发布于 2010-05-27 21:57:52
原因是您正在调用TiXmlElement::attrib的int版本,但是您给它提供了一个类型为const::defualtValue的defualtValue &但是,函数需要一个int类型的defaultValue。
发布于 2010-05-27 21:57:55
attrib<T>(key, defaultValue)可能期望它的第一个参数与它的第二个模板参数的类型相同。
换句话说,mRootElement->attrib<T>(key, defaultValue)中的mRootElement->attrib<T>(key, defaultValue)必须与defaultValue具有相同的类型。
https://stackoverflow.com/questions/2925268
复制相似问题