I am writing a c++ application with several complex structs and
I want to read a string and fill those structs by data provided in that text.
But for easier understanding and debugging, i've wrote easy program with same problem.
This is my code:
#include <string>
#include <iostream>
#define FUSION_MAX_VECTOR_SIZE 30
#define BOOST_PHOENIX_USE_V2_OVER_V3
#include <boost/spirit/home/phoenix/bind/bind_function.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
using qi::double_;
using qi::char_;
using qi::lexeme;
using qi::int_;
using qi::lit;
using qi::_1;
using ascii::space;
using phoenix::ref;
using qi::parser;
class Test
{
// Class fields
std::string test_str;
public:
Test(std::string& sample_str)
{
test_str = sample_str;
}
struct fruit
{
std::string name;
std::string color;
};
BOOST_FUSION_ADAPT_STRUCT
(
fruit,
(std::string, name)
(std::string, color)
);
struct person
{
std::string name;
int age;
};
BOOST_FUSION_ADAPT_STRUCT
(
person,
(std::string, name)
(int, age)
);
void received_person(person& p)
{
std::cout << p.name << " with age"<< p.age<< " has been seen!"<<std::endl;
}
void received_fruit(fruit& f)
{
std::cout << f.name<<" is "<<f.color<<std::endl;
}
template <typename Iterator>
struct MyGrammar : boost::spirit::qi::grammar<Iterator, void()>
{
MyGrammar() : MyGrammar::base_type(my_item)
{
my_item = *(fruit[ boost::phoenix::bind(&received_fruit, boost::spirit::_1 )]
|
_person[ boost::phoenix::bind(&received_person, boost::spirit::_1 )]
);
_person = qi::lit('(') >> *(qi::char_ - ',') >> ',' >> qi::int_ >> ')';
_fruit = qi::lit('[') >> *(qi::char_ - ',') >> ',' >> *(qi::char_) >> ']';
}
qi::rule<Iterator, void()> my_item;
qi::rule<Iterator, person()> _person;
qi::rule<Iterator, fruit()> _fruit;
};
void run()
{
typedef std::string::const_iterator iterator;
MyGrammar <std::string::const_iterator> my_grammar;
std::string::const_iterator begin = test_str.begin();
std::string::const_iterator end = test_str.end();
bool result_ = qi::parse(begin, end, my_grammar) && begin == end;
}
};
int main()
{
std::string input("(jane, 23000)(david, 19)(mary, 30)[yello,100][green, 60.6][red, 30.5]");
Test test(input);
test.run();
return 0;
}
g++ compiles this code and generates this error:
expected unqualified-id before 'namespace'
i know this code can be written without using class, but i want to use class in main project. many thanks in advance!