RapidXML compilation error parsing string

我只是一个虾纸丫 提交于 2019-12-07 16:29:27

问题


I have been having some trouble using RapidXML to parse a string. I receive an error from within Eclipse claiming the parse function does not exist.

make all 
Building file: ../search.cpp
Invoking: Cross G++ Compiler
g++ -DDEBUG -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"search.d" -MT"search.d" -o "search.o" "../search.cpp"
../search.cpp: In function ‘void search(CURL*, CURLcode, std::string, std::string)’:
../search.cpp:29:27: error: no matching function for call to ‘rapidxml::xml_document<>::parse(const char*)’
../search.cpp:29:27: note: candidate is:
../rapidxml-1.13/rapidxml.hpp:1381:14: note: template<int Flags> void rapidxml::xml_document::parse(Ch*) [with int Flags = Flags, Ch = char]
make: *** [search.o] Error 1

The following code raises an error:

rapidxml::xml_document<> doc;    // This has no errors
doc.parse<0>(data.c_str());      // This line raises the error (data is a string)

For reference here is the online documentation: http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1parsing

RapidXML comes as four header files:

  1. rapidxml_iterators.hpp
  2. rapidxml_print.hpp <--contains errors, but build is successful with them
  3. rapidxml_utils.hpp <--contains errors, but build is successful with them
  4. rapidxml.hpp <--linked by program, contains parse function

How do I resolve the error in my code, and do I first need so resolve compiler errors in the headers somehow?


回答1:


The problem is that the char* returned from calling c_str() on a std::string is actually a const char* which is no good for the parse function (parsing actually changes the string that it parses in rapidXML). This means we need to copy the string before we parse it

  xml_document<> doc;
  string str;                             // String you want to parse
  char* cstr = new char[str.size() + 1];  // Create char buffer to store string copy
  strcpy (cstr, str.c_str());             // Copy string into char buffer

  doc.parse<0>(cstr);                     // Pass the non-const char* to parse()

  // Do stuff with parsing

  delete [] cstr;                         // free buffer memory when all is finished

I have not attempted to compile the above so there maybe errors, the point is that c_str() returns a const char* and parse() must take a non-const char*. Hope this helps. As for your headers, I usually get away with only using

 rapidxml.hpp
 rapidxml_print.hpp

included in my source files. You have no linker issues because RapidXML is header only implementation (which makes it nice in my opinion).



来源:https://stackoverflow.com/questions/11604818/rapidxml-compilation-error-parsing-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!