I take some codes from http://boost-spirit.com/home/articles/karma-examples/output-generation-from-a-list-of-key-value-pairs-using-spirit-karma/
But it wouldn't compile
My compiler : vc++ 2010
os : win7 64bits

#include<iostream>
#include<string>

#include<boost/optional.hpp>

#include<boost/spirit/include/karma.hpp>

namespace karma = boost::spirit::karma;
namespace phoenix = boost::phoenix;


int main()
{                           
    typedef std::back_insert_iterator<std::string> sink_type;   
    karma::rule<sink_type, std::string()> base = karma::string;

    typedef std::pair<std::string, boost::optional<std::string> > pair_type;
    karma::rule<sink_type, pair_type()> pair;
    pair  =  karma::string << -('=' << karma::string);  //this line can't be compiled

    std::cout<<"system pause"<<std::endl;
    std::cin.get();

    return 0;
}

error message of vc++ 2010
error C2146: syntax error : missing ';' before identifier 'type'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2208: 'boost::type' : no members defined using this type
fatal error C1903: unable to recover from previous error(s); stopping compilation

What is going on and how could I solve it?
Thanks

For Spirit to be able to parse data directly into a class/struct/union, a fusion adapter is required.
See: http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/tutorials/employee___parsing_into_structs.html

For std::pair<> support, just: [B]#include <boost/fusion/include/std_pair.hpp>[/B] which would pull in (along with some other stuff) BOOST_FUSION_ADAPT_STRUCT( std::pair, ( std::string, first ), ( std::string, second ) ) ;

Thanks for your reply, I also saw another example without the help of boost::fusion

#include <boost/spirit/include/qi.hpp>  
#include <boost/spirit/include/phoenix.hpp>  
#include <iostream>  
#include <string>  
#include <vector>  
#include <map>  

char const *CODE_CPP_KEYWORD_ENUM = "enum";  

namespace haha  
{  
    struct CPPCodeEnum  
    {          
        std::string enumName;      
        std::vector<::std::string> enumMembers;  
    };  
}  

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

    template <typename Iterator>  
    struct CPPCodeEnumGrammar   
        : qi::grammar<Iterator, CPPCodeEnum(),ascii::space_type >  
    {  
        CPPCodeEnumGrammar()   
            : CPPCodeEnumGrammar::base_type(start)  
        {  
            using qi::_val;  
            using qi::_1;  
            using qi::lit;  
            using qi::lexeme;  
            using qi::raw;  
            using qi::space;  
            using ascii::char_;  
            using ascii::string;  
            using phoenix::push_back;  

            quoted_string = lexeme[+(qi::alpha | qi::digit | char_('_'))];  

            start =  
               
                lit(CODE_CPP_KEYWORD_ENUM)>>  
           
                *quoted_string[ phoenix::bind(&CPPCodeEnum::enumName, _val)= _1]>>  
                char_("{")>>  
               
                *(quoted_string[push_back(phoenix::bind(&CPPCodeEnum::enumMembers, _val), _1)]%',')>>              
                -char_(",")>>  
                char_("}");  

        }  

        qi::rule<Iterator, std::string(),ascii::space_type > quoted_string;  
        qi::rule<Iterator, CPPCodeEnum(), ascii::space_type > start;  
    };  
}  

void spirit_enum_parser()  
{          
    ::std::string teststr = "enum myename {m1,m2  ,m3 ,m4 ,}";  

    using boost::spirit::ascii::space;  

    std::string::const_iterator iter = teststr.begin();  
    std::string::const_iterator end = teststr.end();  

    haha::CPPCodeEnumGrammar<std::string::const_iterator> myCPPCodeEnumGrammar;  
    haha::CPPCodeEnum data;  

    bool r = phrase_parse(iter, end, myCPPCodeEnumGrammar,  space, data);   
}

it still work, with boost::phoenix, but I don't know how to apply it on
spirit::karma

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.