Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ TinyXML和获取值_C++_Templates_Tinyxml - Fatal编程技术网

C++ TinyXML和获取值

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

我试图用TinyXML(c++)从xml文件加载数据

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