我是否正确使用了这个类?未解析的外部符号
Am I using this class correctly? Unresolved external symbol
嘿,我是一个编程菜鸟,但我正在使用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
.你一定错过了这些台词。
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 错误 LNK2019:未解析的外部符号"外部"C"此错误是什么?
- 错误 : LNK2019: 未解析的外部符号"外部 "C"