我正在尝试从C++编程语言第10,14和15章实现计算器。
我的链接器有问题,当我尝试运行它时,出现此错误:
未定义对Lexer::ts的引用
我已经尝试了很多方法来摆脱它,但是我不能。谢谢您的帮助!
直流电
namespace Lexer{
struct Token{..};
class Token_stream{...};
extern Token_stream ts;
}
lexer.cpp
#include "dc.h"
Lexer::Token_stream ts{&cin};
解析器
#include "dc.h"
using Lexer::ts;
double Parser::prim(bool get){
if(get) ts.get();
main.cpp
#include "dc.h"
#include <sstream>
using std::string; using std::cout; using std::cin;
using Lexer::ts;
void Driver::calculate() {
for (;;) {
ts.get();
if (ts.current().kind == Lexer::Kind::end) break;
if (ts.current().kind == Lexer::Kind::print)continue;
cout << Parser::expr(false) << '\n';
}
}
int main(int argc, char* argv[]){
Table::table["pi"]=3.14159265;
Table::table["e"]=2.718281828;
Driver::calculate();
return Error::no_of_errors;
}
IM获取错误:
/calculator-src/main.cpp:10: undefined reference to `Lexer::ts'
/calculator-src/main.cpp:11: undefined reference to `Lexer::ts'
/calculator-src/main.cpp:12: undefined reference to `Lexer::ts'
CMakeFiles/DeskCalculator.dir/parser.cpp.o: In function `Parser::prim(bool)':
/calculator-src/parser.cpp:6: undefined reference to `Lexer::ts'
/calculator-src/parser.cpp:8: undefined reference to `Lexer::ts'
最佳答案
Lexer::Token_stream ts{&cin};
这是在全局 namespace 中声明的类型为
ts
的Lexer::Token_stream
变量的声明。您在
namespace Lexer {
中忘记了}
和周围的lexer.cpp
。
关于c++ - DeskTop计算器出现问题。 C++程式语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58581440/