TinyXML 和获取值
我正在尝试使用 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);
}
I'm trying to load data from xml-file with TinyXML (c++).
int height = rootElem->attrib<int>("height", 480);
rootElem is a root element of loaded xml-file. I want to load height value from it (integer). But I have a wrapper function for this stuff:
template<typename T>
T getValue(const string &key, const string &defaultValue = "")
{
return mRootElement->attrib<T>(key, defaultValue);
}
It works with string:
std::string temp = getValue<std::string>("width");
And it fails during fetching:
int temp = getValue<int>("width");
>no matching function for call to ‘TiXmlElement::attrib(const std::string&, const std::string&)’
UPD: The new version of code:
template<typename T>
T getValue(const string &key, const T &defaultValue = T())
{
return mRootElement->attrib<T>(key, defaultValue);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是您正在调用 TiXmlElement::attrib 的 int 版本,但您给它一个 const std::string & 类型的 defualtValue,但是,该函数需要 int 类型的 defaultValue。
The reason is you are calling the int version of TiXmlElement::attrib, but you are giving it a defualtValue of type const std::string &, however, the function expects a defaultValue of type int.
attrib(key, defaultValue)
可能期望它的第一个参数与第二个模板参数的类型相同。换句话说;
mRootElement->attrib(key, defaultValue)
中的T
必须与defaultValue
类型相同。attrib<T>(key, defaultValue)
probably expect it's first argument to be of same type as it's second template argument.In other words;
T
inmRootElement->attrib<T>(key, defaultValue)
must be of the same type asdefaultValue
.