TinyXML 和获取值

发布于 2024-09-03 02:55:00 字数 941 浏览 8 评论 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);
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-09-10 02:55:00

原因是您正在调用 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.

情何以堪。 2024-09-10 02:55:00

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 in mRootElement->attrib<T>(key, defaultValue) must be of the same type as defaultValue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文