[英]Linking error. I don't know what I'm missing
I'm learning programming from Stroustrup's, Programming: Practices & Principles 2nd ed.我正在从 Stroustrup 的《编程:实践与原则》第二版中学习编程。 From chapter.
从章。 6.3.4, I'm using the following code from the book and get the "Undefined symbols for architecture x86_64" error:
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?
}
}
}
The 'std_lib_facitilies.h' doesn't fix the problem. 'std_lib_facitilies.h' 不能解决问题。 For function 'get_token();', am I missing any linking or is it that I can't leave the function blank (as written in the book)?
对于函数“get_token();”,我是否缺少任何链接,还是我不能将该函数留空(如书中所述)? I'm new to programming and any help will be appreciated.
我是编程新手,任何帮助将不胜感激。 I'm using Clang set to: c++11, libc++.
我正在使用 Clang 设置为:c++11、libc++。 Error:
错误:
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)
You need to implement get_token().您需要实现 get_token()。 In your case I'd try to do this within a constructor, like this:
在您的情况下,我会尝试在构造函数中执行此操作,如下所示:
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.
};
Then you could write Token t(cin);
然后你可以写
Token t(cin);
instead of Token t = get_token();
而不是
Token t = get_token();
But still I wouldn't like this implementation.但我仍然不喜欢这个实现。 To make
vector<Token>
work well, it should have a move constructor.为了使
vector<Token>
良好,它应该有一个移动构造函数。 That would enable you to write while(cin) tok.push_back(Token(cin));
这将使您能够编写
while(cin) tok.push_back(Token(cin));
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.