【问题标题】:Problem with a DeskTop Calculator. C++ Programming Language桌面计算器的问题。 C++ 编程语言
【发布时间】:2019-10-27 16:51:05
【问题描述】:

我正在尝试从 C++ 编程语言第 10、14 和 15 章实现一个计算器。

我的链接器有问题,当我尝试运行它时出现此错误:

对 `Lexer::ts' 的未定义引用

我已经尝试了很多方法来摆脱它,但我做不到。感谢您的帮助!

dc.h

namespace Lexer{
    struct Token{..};
    class Token_stream{...};
    extern Token_stream ts;
}

lexer.cpp

#include "dc.h"

Lexer::Token_stream ts{&cin};

解析器.cpp

#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;
}

我得到的错误:

/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'

【问题讨论】:

  • 那个庞然大物的骗子不是很有用山姆
  • 下次展示minimal reproducible example,而不是到处都是“...”的虚构的东西

标签: c++ calculator extern


【解决方案1】:
Lexer::Token_stream ts{&cin};

这是一个名为 ts 的变量的声明,类型为 Lexer::Token_stream在全局命名空间中

您在lexer.cpp 中忘记了namespace Lexer {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2012-05-14
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多