问题描述
我遇到了使用 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 是传递给函数 XMLElement
的 xmlreadLight
。不确定我的问题是否设置正确,所以如果缺少某些信息,请告诉我。
解决方法
根据错误消息,您似乎需要执行以下操作:
12
推测 #include <iostream>
int main()
{
char digit = '0';
std::cout << digit << '\n'; // will print 0 (followed by newline)
std::cout << (int)digit << '\n'; // for an ASCII character set will print 48
std::string test = "12";
int incorrect_sum = 0;
int correct_sum = 0;
for (const auto c : test)
{
incorrect_sum *= 10;
incorrect_sum += c;
correct_sum *= 10;
correct_sum += c - '0'; // note the correction here
}
std::cout << incorrect_sum << ' ' << correct_sum << '\n'; // will print 540 and 12
}
的返回类型已经在 tinyxml2 中设置为 for (const XMLAttribute* a = light->FirstAttribute(); a ; a = a->Next()) { ...
^^^^^
。
如果您在 Github 存储库的第 1513 行检查 tinyxml2.h
文件,您将看到:
FirstAttribute