Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:15:52+00:00 2026-05-27T23:15:52+00:00

In the boost::spirit documentation, grammars are defined by using struct. For example, template <typename

  • 0

In the boost::spirit documentation, grammars are defined by using struct. For example,

template <typename Iterator>
struct my_grammar
    : qi::grammar<Iterator, qi::locals<std::string>, ascii::space_type >
{
    my_grammar() 
      : my_grammar::base_type(start, "start")
    {
       // using this and that
       // rules and action etc.
    }
 };

I am wondering if I can write it using class (if not then why?). I am doing this.

In header file

 template<typename Iterator>
 class my_grammar
 {
     public:
        my_grammar();
        // rules declaration goes here.
  };

and in source file

 template<typename Iterator>
 my_grammar::my_grammar()
       : qi::grammar::base_type(start, "start")
  {
        // write grammar and actions.
  }

Name-space has been shorted using typedefs. I am writing using above method and compiler is giving me too much of errors which are hard to understand. Is it fundamentally ok or I am doing something odd?

Can you point me to some code where someone has used class instead of struct to write grammar?

UPDATE :

I am not able to link now. It says that undefined reference todimacs_grammar<__gnu_cxx::__normal_iterator, std::allocator > > >::my_grammar()` . The problem is that while using struct, I was writing

 my_grammar() 
  : my_grammar::base_type(start, "start")

I am not sure how to write equivalent class declaration and definition for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:15:53+00:00Added an answer on May 27, 2026 at 11:15 pm

    Classes & structs are equivalent except for the default visibility of members (public for structs, private for classes).

    It looks like you forgot to make your my_grammar class derive from the qi::grammar<> base class that you used for your struct. Thus, the base class initializer in your class constructor implementation will look like nonsense to the compiler.

    In short, change your class definition to:

    template<typename Iterator>
    class my_grammar
      : public qi::grammar<Iterator, qi::locals<std::string>, ascii::space_type >
    {
      public:
        my_grammar();
        // rules declaration goes here.
    };
    

    (Note the added public inheritance).

    EDIT (re: linker errror):

    With templates, generally the implementation should live in the header file rather than a source (.cpp) file (though there are exceptions). In your header file, the constructor definition syntax should be:

    template<typename Iterator>
    my_grammar<Iterator>::my_grammar()
      : qi::grammar::base_type(start, "start")
    {
        // write grammar and actions.
    }
    

    (difference is the <Iterator> bit as pointed out by Ken Wayne VanderLinde below).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another problem with my boost::spirit parser. template<typename Iterator> struct expression: qi::grammar<Iterator, ast::expression(),
I remember that when using Boost.Spirit and for the std::function addition to C++0x, you
I am using Boost Spirit parser, and as the parser is parsing, semantic actions
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
I try to learn how Boost.Spirit works. Unfortunately, the documentation is too laconic for
Say I have a Boost Spirit grammar like this, where a parent rule passes
Using Boost.Spirit, is it possible to deliver a terminal token to multiple actions and
I want to parse a file into an AST using Boost Spirit. The root
I'm new to Boost Spirit and trying to write JSON parser using Boost Spirit
I'm trying to parse JSON string using Boost Spirit store JSON object into recursive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.