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

时间:2016-06-03 19:51:10

标签: c++ xcode linker

所以我一直在寻找答案,但似乎找不到一个不专门针对专业图书馆的答案。我目前正在通过Stroustrup的原理和实践学习c ++,虽然我通常在我的电脑上使用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的未定义符号:    &#34; Token_stream :: Token_stream()&#34;,引自:        在main.o中___cxx_global_var_init   ld:找不到架构x86_64的符号   clang:错误:链接器命令失败,退出代码为1(使用-v查看&gt;调用)

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

1 个答案:

答案 0 :(得分:0)

哇我觉得这样的白痴没有意识到这一点,但在发布之后我通过代码阅读并意识到在定义Token_stream类时它是一个冗余。对于读同一个问题的人来说,Stroustrup的例子包括Token Token_stream {}中的Token_stream(),这是错误的。

相关问题