链接错误。我不知道我错过了什么

时间:2015-10-27 20:27:38

标签: c++ linker

我正在学习Stroustrup的编程,编程:实践&原则第二版。从章节。 6.3.4,我正在使用本书中的以下代码并获得“未定义的架构x86_64符号”错误:

#include <iostream>
#include <vector>

using namespace std;

class Token{            //User-defined type with 2 members: Token.
    public:
        char kind;      //Member 'kind'. It has a char type.
        double value;   //Member 'value'. It has a double type.
};

Token get_token();  //Created a BLANK function of Token type to read cin input.
vector<Token> tok;  //Tokens will be placed inside vector 'tok'.    

int main() 
{
    while(cin){
        Token t= get_token();   //t from input.
        tok.push_back(t);   //Value in t is pushed back in the vector.
    }
    for(int i=0;i<tok.size();++i){  //i= 0 until less than size of vector, add 1.
        if(tok[i].kind=='*'){       //Finds '*'.
            double d=tok[i-1].value*tok[i+1].value; 
            //Evaluates object before '*', multiplied by object after it.
            //Now what?
        }
    }
}

'std_lib_facitilies.h'无法解决问题。对于函数'get_token();',我是否缺少任何链接,或者我不能将该函数留空(如书中所述)?我是编程的新手,我们将不胜感激。我正在使用Clang设置为:c ++ 11,libc ++。 错误:

Undefined symbols for architecture x86_64:
  "get_token()", referenced from:
      _main in 6-5f59e7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:0)

您需要实现get_token()。在你的情况下,我试着在构造函数中这样做:

class Token            //User-defined type with 2 members: Token.
{
    public:
        Token(std::istream& inputstream)
        {
            kind << inputstream;
            value << inputstream;
        }
        char kind;      //Member 'kind'. It has a char type.
        double value;   //Member 'value'. It has a double type.
};

然后你可以写Token t(cin);而不是Token t = get_token();

但我仍然不喜欢这种实施方式。为了使vector<Token>运行良好,它应该有一个移动构造函数。这样你就可以写while(cin) tok.push_back(Token(cin));