Boost C++ Libraries: Ticket #9613: Cannot return parse result as a class using boost::spirit::qi https://svn.boost.org/trac10/ticket/9613 <p> Good day! </p> <p> I went through the Spirit tutorials (Boost documentation), chose the "Employee" example (<a href="http://www.boost.org/doc/libs/1_55_0/libs/spirit/example/qi/employee.cpp">http://www.boost.org/doc/libs/1_55_0/libs/spirit/example/qi/employee.cpp</a>) and wanted to return the result of parsing using a class rather than a struct. When just changing from a struct to a class and adding the "public" visibility modifier everything works fine. However when using BOOST_FUSION_ADAPT_ADT, private fields and getters and setters I am receiving compile errors (Source code: below). </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/config/warning_disable.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/spirit/include/qi.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/spirit/include/phoenix_core.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/spirit/include/phoenix_operator.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/spirit/include/phoenix_object.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/fusion/include/io.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/fusion/adapted/adt/adapt_adt.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/fusion/include/adapt_adt.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;complex&gt;</span><span class="cp"></span> <span class="k">namespace</span> <span class="n">client</span> <span class="p">{</span> <span class="k">namespace</span> <span class="n">qi</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">spirit</span><span class="o">::</span><span class="n">qi</span><span class="p">;</span> <span class="k">namespace</span> <span class="n">ascii</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">spirit</span><span class="o">::</span><span class="n">ascii</span><span class="p">;</span> <span class="k">class</span> <span class="nc">employee</span> <span class="p">{</span> <span class="k">private</span><span class="o">:</span> <span class="kt">int</span> <span class="n">age</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">surname</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">forename</span><span class="p">;</span> <span class="kt">double</span> <span class="n">salary</span><span class="p">;</span> <span class="k">public</span><span class="o">:</span> <span class="kt">int</span> <span class="n">getAge</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">age</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">setAge</span><span class="p">(</span><span class="kt">int</span> <span class="n">age</span><span class="p">)</span> <span class="p">{</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">age</span> <span class="o">=</span> <span class="n">age</span><span class="p">;</span> <span class="p">}</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="n">getSurname</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">surname</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">setSurname</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="n">surname</span><span class="p">)</span> <span class="p">{</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">surname</span> <span class="o">=</span> <span class="n">surname</span><span class="p">;</span> <span class="p">}</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="n">getForename</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">surname</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">setForename</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="n">forename</span><span class="p">)</span> <span class="p">{</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">forename</span> <span class="o">=</span> <span class="n">forename</span><span class="p">;</span> <span class="p">}</span> <span class="kt">double</span> <span class="n">getSalary</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">salary</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">setSalary</span><span class="p">(</span><span class="kt">double</span> <span class="n">salary</span><span class="p">)</span> <span class="p">{</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">salary</span> <span class="o">=</span> <span class="n">salary</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> <span class="p">}</span> <span class="n">BOOST_FUSION_ADAPT_ADT</span><span class="p">(</span> <span class="n">client</span><span class="o">::</span><span class="n">employee</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="n">obj</span><span class="p">.</span><span class="n">getAge</span><span class="p">(),</span> <span class="n">obj</span><span class="p">.</span><span class="n">setAge</span><span class="p">(</span><span class="n">val</span><span class="p">))</span> <span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="p">,</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="p">,</span> <span class="n">obj</span><span class="p">.</span><span class="n">getSurname</span><span class="p">(),</span> <span class="n">obj</span><span class="p">.</span><span class="n">setSurname</span><span class="p">(</span><span class="n">val</span><span class="p">))</span> <span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="p">,</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span><span class="p">,</span> <span class="n">obj</span><span class="p">.</span><span class="n">getForename</span><span class="p">(),</span> <span class="n">obj</span><span class="p">.</span><span class="n">setForename</span><span class="p">(</span><span class="n">val</span><span class="p">))</span> <span class="p">(</span><span class="kt">double</span><span class="p">,</span> <span class="kt">double</span><span class="p">,</span> <span class="n">obj</span><span class="p">.</span><span class="n">getSalary</span><span class="p">(),</span> <span class="n">obj</span><span class="p">.</span><span class="n">setSalary</span><span class="p">(</span><span class="n">val</span><span class="p">))</span> <span class="p">)</span> <span class="k">namespace</span> <span class="n">client</span> <span class="p">{</span> <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">Iterator</span><span class="o">&gt;</span> <span class="k">struct</span> <span class="nl">employee_parser</span> <span class="p">:</span> <span class="n">qi</span><span class="o">::</span><span class="n">grammar</span><span class="o">&lt;</span><span class="n">Iterator</span><span class="p">,</span> <span class="n">employee</span><span class="p">(),</span> <span class="n">ascii</span><span class="o">::</span><span class="n">space_type</span><span class="o">&gt;</span> <span class="p">{</span> <span class="n">employee_parser</span><span class="p">()</span> <span class="o">:</span> <span class="n">employee_parser</span><span class="o">::</span><span class="n">base_type</span><span class="p">(</span><span class="n">start</span><span class="p">)</span> <span class="p">{</span> <span class="k">using</span> <span class="n">qi</span><span class="o">::</span><span class="n">int_</span><span class="p">;</span> <span class="k">using</span> <span class="n">qi</span><span class="o">::</span><span class="n">lit</span><span class="p">;</span> <span class="k">using</span> <span class="n">qi</span><span class="o">::</span><span class="n">double_</span><span class="p">;</span> <span class="k">using</span> <span class="n">qi</span><span class="o">::</span><span class="n">lexeme</span><span class="p">;</span> <span class="k">using</span> <span class="n">ascii</span><span class="o">::</span><span class="n">char_</span><span class="p">;</span> <span class="n">quoted_string</span> <span class="o">=</span> <span class="n">lexeme</span><span class="p">[</span><span class="sc">&#39;&quot;&#39;</span> <span class="o">&gt;&gt;</span> <span class="o">+</span><span class="p">(</span><span class="n">char_</span> <span class="o">-</span> <span class="sc">&#39;&quot;&#39;</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="sc">&#39;&quot;&#39;</span><span class="p">];</span> <span class="n">start</span> <span class="o">=</span> <span class="n">lit</span><span class="p">(</span><span class="s">&quot;employee&quot;</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="sc">&#39;{&#39;</span> <span class="o">&gt;&gt;</span> <span class="n">int_</span> <span class="o">&gt;&gt;</span> <span class="sc">&#39;,&#39;</span> <span class="o">&gt;&gt;</span> <span class="n">quoted_string</span> <span class="o">&gt;&gt;</span> <span class="sc">&#39;,&#39;</span> <span class="o">&gt;&gt;</span> <span class="n">quoted_string</span> <span class="o">&gt;&gt;</span> <span class="sc">&#39;,&#39;</span> <span class="o">&gt;&gt;</span> <span class="n">double_</span> <span class="o">&gt;&gt;</span> <span class="sc">&#39;}&#39;</span> <span class="p">;</span> <span class="p">}</span> <span class="n">qi</span><span class="o">::</span><span class="n">rule</span><span class="o">&lt;</span><span class="n">Iterator</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">(),</span> <span class="n">ascii</span><span class="o">::</span><span class="n">space_type</span><span class="o">&gt;</span> <span class="n">quoted_string</span><span class="p">;</span> <span class="n">qi</span><span class="o">::</span><span class="n">rule</span><span class="o">&lt;</span><span class="n">Iterator</span><span class="p">,</span> <span class="n">employee</span><span class="p">(),</span> <span class="n">ascii</span><span class="o">::</span><span class="n">space_type</span><span class="o">&gt;</span> <span class="n">start</span><span class="p">;</span> <span class="p">};</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Give me an employee of the form :&quot;</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;employee{age, </span><span class="se">\&quot;</span><span class="s">surname</span><span class="se">\&quot;</span><span class="s">, </span><span class="se">\&quot;</span><span class="s">forename</span><span class="se">\&quot;</span><span class="s">, salary } </span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="k">using</span> <span class="n">boost</span><span class="o">::</span><span class="n">spirit</span><span class="o">::</span><span class="n">ascii</span><span class="o">::</span><span class="n">space</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">::</span><span class="n">const_iterator</span> <span class="n">iterator_type</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">client</span><span class="o">::</span><span class="n">employee_parser</span><span class="o">&lt;</span><span class="n">iterator_type</span><span class="o">&gt;</span> <span class="n">employee_parser</span><span class="p">;</span> <span class="n">employee_parser</span> <span class="n">g</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">str</span><span class="p">;</span> <span class="n">getline</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cin</span><span class="p">,</span> <span class="n">str</span><span class="p">);</span> <span class="n">client</span><span class="o">::</span><span class="n">employee</span> <span class="n">emp</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">::</span><span class="n">const_iterator</span> <span class="n">iter</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">::</span><span class="n">const_iterator</span> <span class="n">end</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="n">end</span><span class="p">();</span> <span class="kt">bool</span> <span class="n">r</span> <span class="o">=</span> <span class="n">phrase_parse</span><span class="p">(</span><span class="n">iter</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">space</span><span class="p">,</span> <span class="n">emp</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">&amp;&amp;</span> <span class="n">iter</span> <span class="o">==</span> <span class="n">end</span><span class="p">)</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Parsing succeeded</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Parsing failed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9613 Trac 1.4.3 anonymous Thu, 29 Jan 2015 19:33:54 GMT <link>https://svn.boost.org/trac10/ticket/9613#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9613#comment:1</guid> <description> <p> Up ? </p> </description> <category>Ticket</category> </item> <item> <author>Nikita Kniazev <nok.raven@…></author> <pubDate>Sun, 10 Dec 2017 14:16:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9613#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9613#comment:2</guid> <description> <p> The code compiles with the recent boost. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Joel de Guzman</dc:creator> <pubDate>Wed, 13 Dec 2017 22:56:46 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9613#comment:3 https://svn.boost.org/trac10/ticket/9613#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket