Flex-Bison C ++中的对象实例 [英] Instance of object in flex-bison c++
问题描述
我的 main.hpp
看起来像这样:
My main.hpp
looks like this :
#include "json.tab.h"
#include "ob.hpp"
extern Ob *ob;
在我的 test.l
中,我写道:
In my test.l
, I wrote :
%{
#include "main.hpp"
%}
%token KEY
%token COMMA
%%
KEY_SET : KEY {ob->someOp();}
| KEY_SET COMMA KEY {ob->someOp();}
%%
但这给了我:
C:\......c:(.text+0x37a): undefined reference to `ob'
C:\......c:(.text+0x38e): undefined reference to `Ob::someop()'
那么如何在解析器中的任何地方调用该Ob对象?
So how can I call that Ob object from anywhere in my parser?
我的Ob类( Ob.hpp
):
My Ob class(Ob.hpp
) :
#include <bits/stdc++.h>
using namespace std;
#ifndef O_H_
#define O_H_
using namespace std;
class Ob {
public:
Ob();
~Ob();
someop();
};
#endif /*O_H_*/
和 Ob.cpp
:
Ob::someop()
{
cout << "c++ is lit" << endl;
}
现在,我已经将Ob中的所有方法设为静态,因此不需要实例.我的构建规则如下所示:
Now I have made all method in Ob static, so that no need to have an instance. And my build rule looks something like this :
g++ lex.yy.c test.tab.c main.cpp *.hpp -o test.exe
我使解析器生成器变得简单,没有任何方法调用,并且工作正常,没有错误,没有警告:
And I made the parser generator plain, without any method call, and it works fine, no error, no warning :
%%
KEY_SET : KEY
| KEY_SET COMMA KEY
%%
当我添加{Ob::someOp();}
时,它再次给了我相同的错误.
And when I added {Ob::someOp();}
, then it gives me the same error again.
我所有的代码都在这里: https://gist.github.com/maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd/a>
All of my codes are here : https://gist.github.com/maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd
推荐答案
解析器生成器如下所示:
The parser generator looks like this :
%{
#include<stdio.h>
#include "main.h"
#include "json.h"
using namespace Maifee;
%}
...
meaw : INTEGER {Json::ok();}
标题:
#ifndef JSON_H
#define JSON_H
#include <bits/stdc++.h>
using namespace std;
namespace Maifee{
class Json {
public:
static void ok();
};
#endif // JSON_H
cpp文件:
#include <bits/stdc++.h>
#include "json.h"
using namespace std;
using namespace Maifee;
void Json::ok()
{
//whatever here
}
这篇关于Flex-Bison C ++中的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!