-4

Hey I am a noob at programming but I am using the Programming Principles and Practice using C++ by Bjarne Stroustrup. I got to a section where we where creating a simple calculator application and in order to do this I made a Token stream class to deal with the calculator input.

class Token_stream {
  public:
    Token_stream();
    Token get(); 
    void putback(Token t);

  private:
    bool full{ false };
    Token buffer;
};

The problem with this is that I get a linker error (LNK2019) from Token_stream() that says it is a unresolved external symbol. As far as I can tell Token_stream() is the default constructor for that class but I do not know how to resolve this error. The code is exactly the same as in the book also so I am stuck. Do you have any idea of how I could fix this?

Edit here's the entire program:

#include <std_fac.h>


void error(string s1, string s2) {
throw runtime_error(s1 + s2);
}

class Token {
public:
char kind; //What kind of token
double value; //For numbers: a value
Token(char ch)    // make a Token from a char
    :kind(ch), value(0) { }
Token(char ch, double val)     // make a Token from a char and a double
    :kind(ch), value(val) { }
};

class Token_stream {
public:
Token_stream();
Token get();
void putback(Token t);
private:
bool full{ false };
Token buffer;
};

void Token_stream::putback(Token t) {
if (full) error("putback() into a full buffer");
buffer = t;
full = true;
}

Token Token_stream::get() {
if (full) {
    full = false;
    return buffer;
}
char ch;
cin >> ch;
switch (ch) {
case ';': // for print
case 'q': // for quit
case '{': case '}': case '(': case ')': case '+': case'-': case '*': 
case'/':
    return Token{ ch };
case'.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
    cin.putback(ch);
    double val;
    cin >> val;
    return Token('8', val);

}
default:
    error("Bad Token");
}
}

double primary();

double term();

double expression(){
double left = term();
Token_stream ts;
Token t = ts.get();
while(true){
    switch (t.kind) {
    case '+':
        left += term();
        t = ts.get();
        break;
    case '-':
        left -= term();
        t = ts.get();
        break;
    default:
        ts.putback(t);
        return left;
    }
}
return left;
}

double term() { // % functionality needs to be implimented
double left = primary();
Token_stream ts;
Token t = ts.get();
while (true) {
    switch (t.kind) {
    case '*':
        left *= primary();
        t = ts.get();
        break;
    case '/':
    { double d = primary();
    if (d == 0) error("Dividing by zero");
        left /= primary();
        t = ts.get();
        break;
    }
    default:
        ts.putback(t);
        return left;
    }
}
}

double primary() {
Token_stream ts;
Token t = ts.get();
switch(t.kind) {
    case '{':
    { double d = expression();
        t = ts.get();
        if (t.kind != '}') error("'}' expected");
        return d;
    }
    case '(':
    { double d = expression();
    t = ts.get();
    if (t.kind != ')' ) error("')' expected");
    return d;
    }

    case '8':
        return t.value;
    default:
        error("Primary expected");
}
}

int main(){


try {
Token_stream ts;
Token t = ts.get();

while (cin) {
    cout << ">";
    Token t = ts.get();

    if (t.kind == 'q')
    {
        cout << '\n' << "Goodbye!" << '\n';
        keep_window_open();
        return 1;
    }
    if (t.kind == ';') {
        cout << "=" << t.value;
    }
}
}

catch (exception& e) {
    cerr << "error: " << e.what()<< '\n';
    keep_window_open();
    return 1;
}
catch (...) {
    cerr << "Exception \n";
    return 2;
}

return 0;
}
  • May the book aim to show the error? I bet there is a corrected code also somewhere there? Read the section more carefully. – ssovukluk Nov 13 '17 at 01:12
  • Can you post the rest of the code? How is it being built? – roelofs Nov 13 '17 at 01:13
  • I know that book there is not such rest. It goes point by point and discuss the process. I think there is nothing we can do here.. @roelofs – ssovukluk Nov 13 '17 at 01:14
  • I thought the book would fix it as well but moving forward the book asks the reader to test the calculator after making modifications, but none to Token_stream. Not quite sure what the issue is as I thought that a linker error was between files, not all in one file. – thepigdude123 Nov 13 '17 at 01:36

1 Answers1

0

In my copy of the book there is a definition

Token_stream::Token_stream() 
   : full(false), buffer(0)
{ }

at the very end of the page before put_back. You must have missed those lines.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203