C++ TinyXML和获取值
我试图用TinyXML(c++)从xml文件加载数据C++ TinyXML和获取值,c++,templates,tinyxml,C++,Templates,Tinyxml,我试图用TinyXML(c++)从xml文件加载数据 intheight=rootElem->attrib(“高度”,480); rootElem是加载的xml文件的根元素。我想从中加载高度值(整数)。但我有一个包装器函数来包装这些东西: template<typename T> T getValue(const string &key, const string &defaultValue = "") { return mRootElement->a
intheight=rootElem->attrib(“高度”,480);
rootElem是加载的xml文件的根元素。我想从中加载高度值(整数)。但我有一个包装器函数来包装这些东西:
template<typename T>
T getValue(const string &key, const string &defaultValue = "")
{
return mRootElement->attrib<T>(key, defaultValue);
}
模板
T getValue(常量字符串和键,常量字符串和默认值=”)
{
返回mRootElement->attrib(key,defaultValue);
}
它与字符串一起工作:
std::string temp = getValue<std::string>("width");
std::string temp=getValue(“宽度”);
并且在获取过程中失败:
int temp = getValue<int>("width");
>no matching function for call to ‘TiXmlElement::attrib(const std::string&, const std::string&)’
int-temp=getValue(“宽度”);
>调用“tixmlement::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);
}
模板
T getValue(常量字符串和键,常量T&defaultValue=T())
{
返回mRootElement->attrib(key,defaultValue);
}
原因是您正在调用TiXmlElement::attrib的int版本,但是您给它一个const std::string类型的defualtValue&,但是,函数需要int类型的defaultValue。attrib(key,defaultValue)
可能希望它的第一个参数与第二个模板参数的类型相同
换言之<mRootElement->attrib(key,defaultValue)
中的code>T必须与defaultValue
的类型相同
template<typename T>
T getValue(const string &key, const T &defaultValue = T())
{
return mRootElement->attrib<T>(key, defaultValue);
}