【问题标题】:TinyXML and fetching valuesTinyXML 和获取值
【发布时间】:2010-05-27 21:53:27
【问题描述】:

我正在尝试使用 TinyXML (c++) 从 xml 文件加载数据。

int height = rootElem->attrib<int>("height", 480);

rootElem 是加载的 xml 文件的根元素。我想从中加载 height 值(整数)。但我有这个东西的包装函数:

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

【问题讨论】:

    标签: c++ templates tinyxml


    【解决方案1】:

    原因是你调用了 TiXmlElement::attrib 的 int 版本,但是你给它一个 const std::string & 类型的 defualtValue,然而,该函数需要一个 int 类型的 defaultValue。

    【讨论】:

      【解决方案2】:

      attrib&lt;T&gt;(key, defaultValue) 可能希望它的第一个参数与第二个模板参数的类型相同。

      换句话说; T 中的 mRootElement-&gt;attrib&lt;T&gt;(key, defaultValue) 必须与 defaultValue 的类型相同。

      【讨论】:

        猜你喜欢
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多