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