【问题标题】:C++ Linking Error and Undefined Reference in Xcode 7.3.1Xcode 7.3.1 中的 C++ 链接错误和未定义引用
【发布时间】:2016-06-03 19:51:10
【问题描述】:

所以我到处寻找这个问题的答案,但似乎找不到不特定于专业图书馆的答案。我目前正在通过 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_stream()”,引用自: ___cxx_global_var_init 在 main.o ld:未找到架构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看 >invocation)

我认为它希望我在构建阶段链接一些库,但我不知道我需要什么额外的库。另外,我的临时 libc++ 只是因为我不知道如何在 xcode 中#include 标准库,所以如果你觉得有额外的帮助,任何关于这方面的建议也会很棒。谢谢!

【问题讨论】:

  • Token_stream(); -- 那么这个函数在哪里呢?这就是链接器所抱怨的。
  • 所以书错了?你能告诉我这是在哪个页面上吗?
  • @sebenalern 我在 PDF 上,所以页面被搞砸了。这是第 6.8 节,代码是 `class Token_stream { public: Token_stream();令牌获取();无效回退(令牌 t);私人:
  • @PaulMcKenzie 是的,我发现这只是冗余和 Token_stream 引用本身的定义的问题。谢谢!

标签: c++ xcode linker


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-02-13
    • 2015-09-19
    • 2016-06-10
    • 2013-04-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    相关资源
    最近更新 更多