简体   繁体   English

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

[英]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.

相关问题 错误 C2061 语法错误标识符——我不知道我做错了什么 - Error C2061 Syntax Error Identifier -- I don't know what I'm doing wrong 运行时错误。 我知道在哪里,但我不知道为什么以及如何纠正它 - Runtime error. I know where but I don't know why and how to correct it 由于某种原因,我的代码中出现了 [json.exception.type_error.302] 。 我知道错误是什么意思,但我不知道哪里有问题 - I'm getting a [json.exception.type_error.302] for some reason in my code. I know what the error means but I don't know where there's a fault 我不知道是什么导致了错误 (C3074) - I don't know what causing the error (C3074) 我不明白我遇到的valgrind错误 - I don't understand a valgrind error I'm getting 转换为ctypes,但我不知道这些函数在做什么 - Converting to ctypes but I don't know what these functions are doing 我不知道是什么导致了我的代码中的分段错误 - I don't know what is causing the segmentation fault in my code 多个错误 - 不知道我做错了什么 - Multiple errors - Don't know what I am doing wrong 我正在解决一个 leetcode 问题并收到此错误。 我不知道这意味着什么,因为我对 C++ 比较陌生 - I'm solving a leetcode question and getting this error. I have no idea what this mean as I'm relatively new to C++ 向量的结构,我不知道该怎么办 - Vector of structs and I don't know what to do
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM