flex-bison c++ 中对象的实例
Instance of object in flex-bison c++
我的main.hpp
看起来像这样:
#include "json.tab.h"
#include "ob.hpp"
extern Ob *ob;
在我的 test.l
中,我写道:
%{
#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 对象?
我的对象 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中的所有方法都设为静态的,这样就不需要实例了。我的构建规则看起来像这样:
g++ lex.yy.c test.tab.c main.cpp *.hpp -o test.exe
并且我使解析器生成器变得简单,没有任何方法调用,它工作正常,没有错误,没有警告:
%%
KEY_SET : KEY
| KEY_SET COMMA KEY
%%
当我添加 {Ob::someOp();}
时,它又给我同样的错误。
我所有的代码都在这里:https://gist.github.com/maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd
您应该将所有 .cpp
文件而不是 .hpp
文件传递给编译命令。 .hpp
将被预处理器自动包含。如果你不这样做(你的命令中没有包含 Ob.cpp
),那么它就找不到其中包含的函数的定义。
所以你的编译命令应该是这样的:
g++ lex.yy.c test.tab.c main.cpp Ob.cpp -o test.exe
解析器生成器如下所示:
%{
#include<stdio.h>
#include "main.h"
#include "json.h"
using namespace Maifee;
%}
...
meaw : INTEGER {Json::ok();}
Header:
#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
}
我的main.hpp
看起来像这样:
#include "json.tab.h"
#include "ob.hpp"
extern Ob *ob;
在我的 test.l
中,我写道:
%{
#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 对象?
我的对象 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中的所有方法都设为静态的,这样就不需要实例了。我的构建规则看起来像这样:
g++ lex.yy.c test.tab.c main.cpp *.hpp -o test.exe
并且我使解析器生成器变得简单,没有任何方法调用,它工作正常,没有错误,没有警告:
%%
KEY_SET : KEY
| KEY_SET COMMA KEY
%%
当我添加 {Ob::someOp();}
时,它又给我同样的错误。
我所有的代码都在这里:https://gist.github.com/maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd
您应该将所有 .cpp
文件而不是 .hpp
文件传递给编译命令。 .hpp
将被预处理器自动包含。如果你不这样做(你的命令中没有包含 Ob.cpp
),那么它就找不到其中包含的函数的定义。
所以你的编译命令应该是这样的:
g++ lex.yy.c test.tab.c main.cpp Ob.cpp -o test.exe
解析器生成器如下所示:
%{
#include<stdio.h>
#include "main.h"
#include "json.h"
using namespace Maifee;
%}
...
meaw : INTEGER {Json::ok();}
Header:
#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
}