title: C++ Using RapidXml parsing XML File, Wrapper Class, parse_error expect >
body: <p>I'm trying to use the <a href="http://rapidxml.sourceforge.net/" rel="nofollow">RapidXML</a> to parse my XML file. And I did it following the <a href="https://gist.github.com/JSchaenzle/2726944" rel="nofollow">example here</a>. Instead of doing the parsing in the main function, I wrote a wrapper class called XMLParser to do the parsing job. And this really gives me a headache.</p>
  
  <p>The <strong>XMLParser.hpp:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;vector&gt;
  #include "rapidxml/rapidxml.hpp"
  
  using namespace std;
  using namespace rapidxml;
  
  class XMLParser {
  
  public:
      XMLParser() {};
  
      XMLParser(const std::string &amp;xmlString): xmlCharVector(xmlString.begin(), xmlString.end())
      {
          //xmlCharVector.push_back('\0');
           parseXML();
      }
      XMLParser(const std::vector&lt;char&gt; &amp;_xmlVector):xmlCharVector(_xmlVector)
      {
          /* xmlCharVector.push_back('\0'); */  // already done in main.cpp
          if (xmlCharVector != _xmlVector)      //And it turns out they're the same....
              std::cout &lt;&lt; "The two vectors are not equal" &lt;&lt; std::endl;
          else
              std::cout &lt;&lt; "They are the same" &lt;&lt; std::endl;
          parseXML();
      }
  
  private:
      std::vector&lt;char&gt; xmlCharVector;
      rapidxml::xml_document&lt;&gt; doc;
      void parseXML();
  
  };
  </code></pre>
  
  <p>The <strong>XMLParser.cpp:</strong></p>
  
  <pre><code>#include "XMLParser.hpp"
  
  using namespace std;
  using namespace rapidxml;
  
  void XMLParser::parseXML()
  {
      doc.parse&lt;0&gt;(&amp;xmlCharVector[0]);
  }
  </code></pre>
  
  <p>And here is the <strong>main.cpp:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  #include "XMLParser.hpp"
  
  using namespace std;
  using namespace rapidxml;
  
  int main(int argc, char **argv)
  {
      xml_document&lt;&gt; doc;
      xml_node&lt;&gt; *root_node;
      ifstream theFile("beer.xml");
      vector&lt;char&gt; buffer((istreambuf_iterator&lt;char&gt;(theFile)), istreambuf_iterator&lt;char&gt;());
      buffer.push_back('\0');
  
      doc.parse&lt;0&gt;(&amp;buffer[0]);
  
      root_node = doc.first_node("MyBeerJournal");
      xml_node&lt;&gt; *engine = root_node-&gt;first_node("Brewery");
  
      //The above code works pretty well, and I can get the element I want in XML file.
  
      //The problem occurs when I tried to use the XMLParser
      XMLParser xmlParser(buffer);
      return 0;
  }
  </code></pre>
  
  <p>The parsing process in the main function works pretty well. But when I  tried to use the function in my wrapper class <code>parseXML()</code>, then error occured:</p>
  
  <p><strong>terminate called after throwing an instance of 'rapidxml::parse_error'
    what():  expected >
  Abort (core dumped)</strong></p>
  
  <p>Originally I have other code in this function, but I commented them all, and find that even with the single line <code>doc.parse&lt;0&gt;(&amp;xmlCharVector[0]);</code>. Why it works well in main.cpp while not in the wrapper class? I really can't figure it out. Could anybody help me?</p>