0

I am quite new to Spirit. I am trying to use Qi to parse the argument for a CMD command in my embedded Tcl interpreter. As some arguments may used multiple times, I will need a vector to store all arguments of the same sort.

This is a simplified example of my problem, where I try to store integers into a vector.

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/support.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>

using std::string;
using std::vector;

namespace {
  namespace qi = boost::spirit::qi;
  namespace phoenix = boost::phoenix;
  namespace ascii = boost::spirit::ascii;

  struct Argument {
    vector<int> svDefine;    // macro definitions
  };
}

BOOST_FUSION_ADAPT_STRUCT
(
 Argument,
 (vector<int>, svDefine)
 )

namespace {
  typedef string::const_iterator SIter;

  struct ArgParser : qi::grammar<SIter, Argument(), ascii::space_type> {
    qi::rule<SIter, Argument(), ascii::space_type> start;

    ArgParser() : ArgParser::base_type(start) {
      using phoenix::at_c;
      using qi::int_;
      using phoenix::push_back;
      using namespace qi::labels;


      start = +("define" >> int_     [push_back(at_c<0>(_val), _1)]);

    }
  };
}

Compiling it with g++ 4.5.1 boost 1.51 generates lost of errors

In file included from /usr/include/boost/spirit/home/phoenix/container.hpp:10:0,
                 from /usr/include/boost/spirit/home/phoenix.hpp:12,
                 from /usr/include/boost/spirit/include/phoenix.hpp:13,
                 from qi_test.cpp:2:
.....
qi_test.cpp:43:64:   instantiated from here
/usr/include/boost/spirit/home/phoenix/stl/container/container.hpp:492:40: error: ‘struct boost::fusion::vector<int>’ has no member named ‘push_back’
/usr/include/boost/spirit/home/phoenix/stl/container/container.hpp:492:40: error: return-statement with a value, in function returning 'void'
qi_test.cpp: In static member function ‘static 
.....
qi_test.cpp:43:64:   instantiated from here
qi_test.cpp:28:509: error: invalid initialization of reference of type boost::fusion::vector<int>&’ from expression of type ‘std::vector<int>’

Basically I am confused. No idea what is wrong.

danlei
  • 14,121
  • 5
  • 58
  • 82
Wei Song
  • 545
  • 3
  • 11
  • 1
    There are two different `vector` types in the error output. In your code, try to be explicit about which vector you're using, i.e. change the declaration in `struct Argument` to `std::vector...`. – Stefan Majewsky Sep 05 '12 at 15:35
  • Unbelievable! @StefanMajewsky, you are right. It is another name space conflict. Would you like to write a short answer for it, as then I can close the question. – Wei Song Sep 05 '12 at 15:40

1 Answers1

2

You are mixing up two types of vector template classes here:

std::vector

and

boost::fusion::vector

if you just omit (comment out)

using std::vector;

things will probably become quite clear

RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92