Xcode 7.3.1中的C++链接错误和未定义引用

C++ Linking Error and Undefined Reference in Xcode 7.3.1

本文关键字:错误 未定义 引用 链接 C++ 中的 Xcode      更新时间:2023-10-16

所以我到处寻找这个问题的答案,但似乎找不到一个不是专门库的答案。我目前正在通过Stroustrup的原理和实践学习c++,虽然我通常在PC上使用visual studio,但我想在mac上用Xcode测试一下。出于某种原因,下面的代码出现了这篇文章底部复制的两个错误。

#include "libc++.h" // just a header file with the std library info copied from stroustrup's website
using namespace std;
string error(){
string s;
throw runtime_error(s);
}
class Token{
public:
char kind;
double value;
};
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("Full putback()");
buffer = t;
full = true;
}
Token Token_stream::get(){
if(full){
full = false;
return buffer;
}
char ch;
cin >> ch;
switch(ch){
case ';':
case 'q':
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");
}
return Token{0};
}
double expression();
Token_stream ts;
double primary(){
Token t = ts.get();
while(true){
switch (t.kind) {
case '(':{
double d = expression();
t = ts.get();
if(t.kind != ')') error("Expected ')'");
return d;
}
case 8:
return t.value;
default:
error("Primary value expected");
}
}

}
double term(){
double left = primary();
Token t = ts.get();
while(true){
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
break;
case '/':{
double d = primary();
if(d == 0) error("Bad input: Can not divide by zero.");
left /= primary();
t = ts.get();
}
default:
ts.putback(t);
return left;
}
}
}
double expression(){
double left = term();
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;
}
}
}


int main()
try {
while(cin)
cout << '=' << expression() << 'n';
}
catch(exception& e){
cerr << "error: " << e.what() << 'n';
}

我知道这并没有很好地记录下来,但这只是我在复习书中的练习。每当我尝试构建时,我都会出现这两个错误。

体系结构x86_64的未定义符号:"Token_stream::Token_sttream()",引用自:___cxx_global_var_init在main.o中ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看>调用)

我想它希望我在构建阶段链接一些库,但我不知道我需要什么额外的库。此外,我的临时libc++只是因为我不知道如何在xcode中包含标准库,所以如果你觉得有额外的帮助,任何关于这方面的建议都会很好。非常感谢。

哇,我觉得自己像个白痴,因为我没有意识到这一点,但在发布后,我通读了代码,并在定义Token_stream类时意识到这是一个冗余。对于任何有同样问题的人来说,Stroustrup的例子在Token_stream{}中包含了Token_sttream(),这是错误的。