簡體   English   中英

桌面計算器的問題。 C++ 編程語言

[英]Problem with a DeskTop Calculator. C++ Programming Language

我正在嘗試從 C++ 編程語言第 10,14 和 15 章實現一個計算器。

我對 linker 有問題,當我嘗試運行它時,我收到此錯誤:

對“Lexer::ts”的未定義引用

我已經嘗試了很多方法來擺脫它,但我不能。 謝謝您的幫助!

直流.h

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

詞法分析器

#include "dc.h"

Lexer::Token_stream ts{&cin};

解析器.cpp

#include "dc.h"
using Lexer::ts;
double Parser::prim(bool get){
    if(get) ts.get();

主文件

#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'
Lexer::Token_stream ts{&cin};

這是一個名為ts的變量的聲明,類型為Lexer::Token_stream位於全局命名空間中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM