boost::spirit 中的类 v/s 结构
在 boost::spirit 文档中,语法是使用 struct 定义的。例如,
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.
}
};
我想知道是否可以使用类来编写它(如果不能那么为什么?)。我正在做这个。
在头文件
template<typename Iterator>
class my_grammar
{
public:
my_grammar();
// rules declaration goes here.
};
和源文件中,
template<typename Iterator>
my_grammar::my_grammar()
: qi::grammar::base_type(start, "start")
{
// write grammar and actions.
}
名称空间已使用 typedef 缩短。我正在使用上述方法编写,编译器给了我太多难以理解的错误。基本上没问题还是我做了一些奇怪的事情?
你能给我指出一些代码,其中有人使用类而不是结构来编写语法吗?
更新:
我现在无法链接。它表示对 dimacs_grammar<__gnu_cxx::__normal_iterator, std::allocator > 的未定义引用> >::my_grammar()` 。问题是,在使用结构时,我在写时
my_grammar()
: my_grammar::base_type(start, "start")
不确定如何为此编写等效的类声明和定义?
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 to
dimacs_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
课程&除了成员的默认可见性(结构为公共,类为私有)之外,结构是等效的。
看起来您忘记让您的
my_grammar
类派生自您用于结构的qi::grammar<>
基类。因此,类构造函数实现中的基类初始值设定项对于编译器来说看起来毫无意义。简而言之,将您的类定义更改为:(
注意添加的公共继承)。
编辑(回复:链接器错误):
对于模板,通常实现应位于头文件中,而不是源 (.cpp) 文件中(尽管也有例外)。在头文件中,构造函数定义语法应该是:(
区别在于
位,如下面 Ken Wayne VanderLinde 所指出的)。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 theqi::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:
(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:
(difference is the
<Iterator>
bit as pointed out by Ken Wayne VanderLinde below).我看到的唯一问题是,
。
如果您希望它与您的
struct
相同另外,你的
class
可能应该像The only problem I see is that
should be
if you want it to be the same as for your
struct
.Also, your
class
should probably be like