繁体   English   中英

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

[英]Linking error. I don't know what I'm missing

我正在从 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)

您需要实现 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)); .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM