我是否正确使用了这个类?未解析的外部符号

Am I using this class correctly? Unresolved external symbol

本文关键字:符号 外部 是否      更新时间:2023-10-16

嘿,我是一个编程菜鸟,但我正在使用Bjarne Stroustrup的编程原理和实践C++。 我到了创建一个简单的计算器应用程序的部分,为了做到这一点,我制作了一个 Token 流类来处理计算器输入。

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

这样做的问题是我从Token_stream()那里得到一个链接器错误(LNK2019(,说它是一个未解析的外部符号。 据我所知,Token_stream()是该类的默认构造函数,但我不知道如何解决此错误。 代码也与书中完全相同,所以我被卡住了。 你知道我该如何解决这个问题吗?

编辑这是整个程序:

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

在我的书中有一个定义

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

在页面的最后,put_back.你一定错过了这些台词。

相关文章: