a simple boost.spirit function compile error!

Discussion in 'C++' started by truelixin@gmail.com, Mar 26, 2009.

  1. Guest

    1>
    2> #include <boost/spirit/core.hpp>
    3>
    4> using namespace boost::spirit;
    5>
    6> rule<> expr, identity;
    7>
    8> expr = identity % '+';
    9> identity = ch_p('X') | ch_p('Y');
    10>
    11> int main()
    12> {
    13> parse_info<> info = parse("X+Y", expr);
    14> }

    Just this simple program, I use VC8, but can't compile it.

    ------ Build started: Project: BoostSpiritTest, Configuration: Debug
    Win32 ------
    Compiling...
    main.cpp
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(8) : error C4430: missing type specifier -
    int assumed. Note: C++ does not support default-int
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(8) : error C2371: 'expr' : redefinition;
    different basic types
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(6) : see declaration of 'expr'
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(8) : error C2440: 'initializing' : cannot
    convert from 'boost::spirit::sequence<A,B>' to 'int'
    with
    [
    A=boost::spirit::rule<>,

    B=boost::spirit::kleene_star<boost::spirit::sequence<boost::spirit::chlit<char>,boost::spirit::rule<>>>
    ]
    No user-defined-conversion operator available that can perform
    this conversion, or the operator cannot be called
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(9) : error C4430: missing type specifier -
    int assumed. Note: C++ does not support default-int
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(9) : error C2371: 'identity' : redefinition;
    different basic types
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(6) : see declaration of 'identity'
    d:\mydocuments\visual studio 2005\projects\boostspirittest
    \boostspirittest\main.cpp(9) : error C2440: 'initializing' : cannot
    convert from 'boost::spirit::alternative<A,B>' to 'int'
    with
    [
    A=boost::spirit::chlit<char>,
    B=boost::spirit::chlit<char>
    ]
    No user-defined-conversion operator available that can perform
    this conversion, or the operator cannot be called
    Build log was saved at "file://d:\MyDocuments\Visual Studio
    2005\Projects\BoostSpiritTest\BoostSpiritTest\Debug\BuildLog.htm"
    BoostSpiritTest - 6 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
    ==========
    , Mar 26, 2009
    #1
    1. Advertising

  2. * :
    > 1>
    > 2> #include <boost/spirit/core.hpp>
    > 3>
    > 4> using namespace boost::spirit;
    > 5>
    > 6> rule<> expr, identity;
    > 7>
    > 8> expr = identity % '+';
    > 9> identity = ch_p('X') | ch_p('Y');


    You can't have assignments or other commands (statements that are not
    declarations) directly at namespace scope.


    > 10>
    > 11> int main()
    > 12> {


    But you could have had them here.


    > 13> parse_info<> info = parse("X+Y", expr);
    > 14> }


    I suggest waiting a little in trying out Boost Spirit, because it's a library
    that uses and requires you to deal with advanced language features.

    The error above is, in contrast, a typical novice error.

    It may be that you have experience with other programming languages (e.g. your
    clean 'main' seems to indicate that) and so feel that some "real" challenge such
    as using Boost Spirit is just what you need to learn things, but still, if that
    is the case, I recommend trying out simpler things first so as to learn the
    basics of the language -- otherwise you'll be stumped at every turn.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
    Alf P. Steinbach, Mar 26, 2009
    #2
    1. Advertising

  3. Alf P. Steinbach wrote:
    > * :
    >> 1>
    >> 2> #include <boost/spirit/core.hpp>
    >> 3>
    >> 4> using namespace boost::spirit;
    >> 5>
    >> 6> rule<> expr, identity;
    >> 7>
    >> 8> expr = identity % '+';
    >> 9> identity = ch_p('X') | ch_p('Y');

    >
    > You can't have assignments or other commands (statements that are not
    > declarations) directly at namespace scope.


    It looks like the OP wanted to initialise them and instead assigned to
    them. Perhaps this would work:

    rule<> identity = ch_p('X') | ch_p('Y'),
    expr = identity % '+', i;

    >
    >
    >> 10>
    >> 11> int main()
    >> 12> {

    >
    > But you could have had them here.


    I am not familiar with Boost Spirit, but for may objects initialisation
    works, so I dare say that the OP just as well could have had them there,
    had he/she "combined the statements"...

    >
    >
    >> 13> parse_info<> info = parse("X+Y", expr);
    >> 14> }

    >
    > I suggest waiting a little in trying out Boost Spirit, because it's a
    > library that uses and requires you to deal with advanced language features.
    >
    > The error above is, in contrast, a typical novice error.
    >
    > It may be that you have experience with other programming languages
    > (e.g. your clean 'main' seems to indicate that) and so feel that some
    > "real" challenge such as using Boost Spirit is just what you need to
    > learn things, but still, if that is the case, I recommend trying out
    > simpler things first so as to learn the basics of the language --
    > otherwise you'll be stumped at every turn.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 26, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ingo Nolden
    Replies:
    1
    Views:
    475
    Rob Williscroft
    Jul 14, 2004
  2. Evan Carew

    Boost Spirit Question

    Evan Carew, Nov 4, 2004, in forum: C++
    Replies:
    4
    Views:
    431
    Evan Carew
    Nov 4, 2004
  3. Pete Becker
    Replies:
    0
    Views:
    1,373
    Pete Becker
    Feb 10, 2005
  4. Fab
    Replies:
    21
    Views:
    6,419
    Simon Brooke
    Nov 16, 2006
  5. Fab
    Replies:
    1
    Views:
    351
Loading...

Share This Page