RapidXML编译错误解析字符串 [英] RapidXML compilation error parsing string

查看:460
本文介绍了RapidXML编译错误解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RapidXML解析字符串时遇到了一些麻烦。我在Eclipse中收到一个错误,声称解析函数不存在。

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

以下代码引发错误:

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


http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1解析

RapidXML有四个头文件:

RapidXML comes as four header files:


  1. rapidxml_iterators.hpp

  2. rapidxml_print.hpp< - 包含错误,但构建成功。

  3. rapidxml_utils.hpp< - 包含错误,但构建成功

  4. rapidxml.hpp < - 按程序链接,包含解析函数

  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?

推荐答案

是在上调用 c_str()返回 char * std :: string 实际上是一个 const char * 这对解析函数没有好处(解析实际上改变了在rapidXML中解析的字符串)。这意味着我们需要在解析之前复制字符串。

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

我没有尝试编译上面的内容, ,关键是 c_str()返回一个 const char * parse / code>必须采用非const char * 。希望这可以帮助。至于您的标头,我通常只使用

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

包含在我的源文件中。你没有链接器问题,因为RapidXML是只有头的实现(这使得它在我看来很好)。

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

这篇关于RapidXML编译错误解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆