Problem with a DeskTop Calculator. C++ Programming Language

I'm trying to implement a calculator from the C++ Programming Language, chapter 10,14 and 15.

I have a problem with the linker, when I try to run it I get this error:

undefined reference to `Lexer::ts'

I have try plenty of things to get rid of it, but I cant. Thanks for the help!

dc.h

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

lexer.cpp

#include "dc.h"

Lexer::Token_stream ts{&cin};

parser.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;
}

ERRORS IM GETTING:

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

Upvotes: 1

Views: 147

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

Lexer::Token_stream ts{&cin};

That's a declaration of a variable called ts, of type Lexer::Token_stream, in the global namespace.

You forgot namespace Lexer { and } around it in lexer.cpp.

Upvotes: 1

Related Questions