gpt4 book ai didi

c++ - 使用 xerces for c++ 调用 getDocument 时出现段错误

转载 作者:行者123 更新时间:2023-11-28 03:45:33 26 4
gpt4 key购买 nike

美好的一天。我有 parser.h sample.cpp(作为父节点)和 main.cpp。我越来越获取 getDocument 时出现段错误。有人能帮帮我吗……这是我的代码:

解析器.h

int Parser::Load(const char* xmlFile) {  
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());

cout << "Error during initialization! :\n"
<< message << "\n";

XMLString::release(&message);
return 1;
}

XercesDOMParser* parser = new XercesDOMParser();
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true);
ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(errHandler);

try {
parser->parse(xmlFile);
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());

cout << "Exception message is: \n"
<< message << "\n";

XMLString::release(&message);
return -1;
}
catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);

cout << "Exception message is: \n"
<< message << "\n";

XMLString::release(&message);
return -1;
}
catch (...) {
cout << "Unexpected Exception \n";
return -1;
}
}

示例.cpp

void Sample::getXmlNodes(ParseXml* xmlParser){  
map<std::string, std::string> attribList;
xmlParser->xmlDoc = xmlParser->parser->getDocument();
xmlParser->xmlNode = xmlParser->xmlDoc->getFirstChild();
attribList = xmlParser->getNodeAttribute(xmlParser->xmlNode);
}

main.cpp

#include "parser.h"  
#include "sample.cpp"

int main(int argc,char **argv){
const char* xmlString="sample.xml";
Parser* xmlObj;
Sample sampObj;

xmlObj->Load(xmlString);
sampObj.getXmlNodes(xmlObj);

cout << "Done creating object..." << endl;

xmlObj->Cleanup();
return 0;
}

我不知道我的代码有什么问题......

最佳答案

您没有为xmlObj objetc 分配内存:

Parser* xmlObj;
Sample sampObj;
xmlObj->Load(xmlString); // SIGSEV here, you have no object to invoke

必须替换为:

Parser xmlObj;  
Sample sampObj;
xmlObj.Load(xmlString);
sampObj.getXmlNodes(&xmlObj);

关于c++ - 使用 xerces for c++ 调用 getDocument 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com