【问题标题】:What is the equivalent of TiXmlAttribute for tinyxml2 and how to use it?tinyxml2 的 TiXmlAttribute 等价物是什么以及如何使用它?
【发布时间】:2021-05-23 23:28:27
【问题描述】:

我遇到了一个使用 tinyxml2 无法解决的问题。

我有一个函数接收XMLElement 作为参数,我需要迭代它的属性。使用 tinyxml,这很有效:

void xmlreadLight(TiXmlElement* light){
    for (TiXmlAttribute* a = light->FirstAttribute(); a ; a = a->Next()) {
         //Do stuff
    }
}

对 tinyxml2 使用相同的方法,如下例所示,我收到以下错误:

const tinyxml2::XMLAttribute * 类型的值不能用于初始化tinyxml2::XMLAttribute * 类型的实体

void xmlreadLight(XMLElement* light){
    for (XMLAttribute* a = light->FirstAttribute(); a ; a = a->Next()) {
         //Do stuff
    }
}

有问题的 XML 代码是:

<lights>
   <light type="POINT" posX=-1.0 posY=1.0 posZ=-1.0 ambtR=1.0 ambtG=1.0 ambtB=1.0 />
</lights>

其中 light 是传递给函数 xmlreadLightXMLElement。不确定我的问题是否正确设置,所以如果缺少某些信息,请告诉我。

【问题讨论】:

    标签: c++ tinyxml tinyxml2


    【解决方案1】:

    按照错误消息,看起来你需要这样做:

    for (const XMLAttribute* a = light->FirstAttribute(); a ; a = a->Next()) { ...
         ^^^^^
    

    想必FirstAttribute的返回类型在tinyxml2中已经变成const了。


    如果您检查 Github 存储库中第 1513 行的 tinyxml2.h 文件,您将看到:

    /// Return the first attribute in the list.
    const XMLAttribute* FirstAttribute() const {
        return _rootAttribute;
    }
    

    【讨论】:

    • 我试过了,但似乎我错过了一些调试信息。我认为应用程序因此没有运行,即使使用 const,但代码中还有另一个错误。不过谢谢,这正是我需要的解决方案。
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多