C++ TinyXML2 EXC\u坏访问我确定这是一个空ptr,但不知道为什么
所以我已经做了好几天了,我不知道为什么会抛出坏的访问错误。有时有效,有时无效C++ TinyXML2 EXC\u坏访问我确定这是一个空ptr,但不知道为什么,c++,xcode,pointers,exc-bad-access,tinyxml2,C++,Xcode,Pointers,Exc Bad Access,Tinyxml2,所以我已经做了好几天了,我不知道为什么会抛出坏的访问错误。有时有效,有时无效 void xmlParser::parseXML(string file){ tinyxml2::XMLDocument doc; if(!doc.LoadFile(file.c_str())) { cout << "ERROR: TINYXML2 FAILED TO LOAD" << endl; } //XML FILE LAYOUT: //<item> // &l
void xmlParser::parseXML(string file){
tinyxml2::XMLDocument doc;
if(!doc.LoadFile(file.c_str()))
{
cout << "ERROR: TINYXML2 FAILED TO LOAD" << endl;
}
//XML FILE LAYOUT:
//<item>
// <type id="laserWeapon" name="Laser Rifle">
// <tooltip>
// <stats>
//</item>
//error seems to occur on this line
tinyxml2::XMLElement* elementType = doc.FirstChildElement("item")->FirstChildElement("type");
string id = elementType->Attribute("id");
string name = elementType->Attribute("name");
cout << "id: " << id << endl;
cout << "name: " << name << endl;
}
void xmlParser::parseXML(字符串文件){
tinyxml2::XMLDocument文档;
如果(!doc.LoadFile(file.c_str()))
{
cout属性(“id”);
字符串名称=元素类型->属性(“名称”);
cout//item元素可能会丢失,您将无法访问。不要以这种方式链接您的调用
tinyxml2::XMLElement*elementType=doc.FirstChildElement(“项目”)->FirstChildElement(“类型”);
//可以忽略元素类型以及属性id和名称
字符串id=元素类型->属性(“id”);
字符串名称=元素类型->属性(“名称”);
谢谢。似乎在FirstChildElement(“项目”)上得到了一个空PTR;
// item element can be missed and you'll get bad access. Do not chain your calls that way
tinyxml2::XMLElement* elementType = doc.FirstChildElement("item")->FirstChildElement("type");
// element type can be missed, as well as attributes id and name
string id = elementType->Attribute("id");
string name = elementType->Attribute("name");
cout << "id: " << id << endl;
cout << "name: " << name << endl;
}