2012-07-22 136 views
3

我一直在使用RapidXML解析字符串時遇到了一些麻煩。我在Eclipse中收到一個錯誤,聲稱解析函數不存在。RapidXML編譯錯誤解析字符串

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_1parsing

RapidXML來自四個頭文件:

  1. rapidxml_iterators。 hpp
  2. rapidxml_print.hpp < --contains錯誤,但構建成功與他們
  3. rapidxml_utils.hpp < --contains錯誤,但構建成功與他們
  4. rapidxml.hpp <通過程序--linked,包含分析功能

如何解決我的代碼中的錯誤,並首先需要如此解決頭中的編譯器錯誤?

回答

5

的問題是,從char*std::string調用c_str()返回實際上是一個const char*這是沒有良好的解析函數(實際上解析改變了字符串,它在rapidXML解析)。這意味着我們需要複製該字符串,我們分析它

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()必須採取非const char*之前。希望這可以幫助。至於您的標題,我通常只能使用

rapidxml.hpp 
rapidxml_print.hpp 

包括在我的源文件中。你沒有鏈接器的問題,因爲RapidXML是頭只實現(這使得它在我看來很好)。

+1

感謝您的支持!這讓我瘋狂! – stephenwebber 2012-08-01 03:51:59

+1

或者,您可以通過調用parse指向字符串第一個成員的指針來避免複製。 'doc.parse <0>(&str [0]);' – Hydranix 2016-12-02 04:28:14

+0

@Hydranix我嘗試了你的方法,但沒有奏效。我仍然試圖找出解析字符串的方法,而不必通過將字符串複製到char數組來複制內存。如果我找到答案,我會在這裏發佈答案。 – 2017-05-22 18:27:31