未定义的参考编译器错误 [英] Undefined Reference Compiler Error

查看:48
本文介绍了未定义的参考编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经接近了,但是我遇到了这个错误,我已经将脑袋撞到墙上好几个小时了.我想念一些愚蠢的东西,我一个又一个地走了,但我找不到.

编译器给我

  main.cpp:16:未定义对`translator :: translator(std :: istream&)'的引用collect2:错误:ld返回1退出状态 

当我尝试编译程序时.我用来编译的命令是:

 清除&&g ++ -g -Wall main.cpp -o梨 

使用的三个部分如下:

main.cpp

  int main(int argc,char * argv []){std :: ifstream myFile;myFile.open(argv [1]);翻译器示例(myFile);myFile.close();返回0;} 

translator.cpp

  #include< fstream>#include< iostream>#include< string>#include"translator.h"译者::译者(std :: istream& in){table1(in);table2(in);} 

translator.h

  #ifndef翻译器#define翻译器#include< fstream>#include< iostream>#include< string>#include"translationTable.h"类翻译器{私人的:translationTable< std :: string,int>表格1;translationTable< int,std :: string>table2;translator();上市:转换器(std :: istream& in);};#万一 

有什么想法吗?我已经做了很多尝试,并且查找了类似的问题,但是它们都有不同的来源.预先感谢!

解决方案

g ++ 的命令行需要包含两个源文件,如下所示:

 <代码> g ++ -g -Wall main.cpp translator.cpp -o梨 

否则,编译器不知道从何处获得 translator :: translator(std :: istream&)成员函数的实现.

* * (摘自评论)

我认为基本上使用头文件是为了使它知道从哪里获取文件的每个实现?

这部分过分简化了,但是应该可以帮助您了解图片.回想一下,从C ++源代码生成可执行文件的过程包括两个主要步骤-编译和链接. g ++ 程序同时执行这两个操作(如果您指定 -c 标志或仅传递 .o 文件,则只能执行一个操作).

g ++ 的编译器阶段和链接器阶段不会直接彼此交谈".编译器为链接器生成输入,这就是通信结束的地方.

头文件用于编译器.具体来说,它们是用于编译的第一阶段-预处理.一旦预处理器完成,就不知道定义的来源.即使编译器也不知道,更不用说链接器了.这就是为什么您需要通过向 g ++ 提供所有相关的源来帮助"链接器.

I think I'm getting close, but I'm having this error I've been banging my head against the wall on for hours. I'm missing something stupid, and I've gone character by character but I can't find it.

The compiler is giving me

main.cpp:16: undefined reference to `translator::translator(std::istream&)'
collect2: error: ld returned 1 exit status

when I try to compile my program. The command I'm using to compile is:

clear && g++ -g -Wall main.cpp -o Pear

The three sections of use are as follows:

main.cpp

int main(int argc, char* argv[])
{

std::ifstream myFile;

myFile.open(argv[1]);

translator example(myFile); 

myFile.close(); 

return 0; 
}

translator.cpp

#include <fstream>
#include <iostream>
#include <string>

#include "translator.h"


translator::translator(std::istream& in)
{   
table1(in);
table2(in);
}

translator.h

#ifndef TRANSLATOR
#define TRANSLATOR

#include <fstream>
#include <iostream>
#include <string>

#include "translationTable.h"


class translator
{
private:

translationTable<std::string, int> table1;

translationTable<int, std::string> table2;

translator(); 

public:

translator(std::istream& in); 

};



#endif

Any ideas? I've tried so much, and I've looked up similar problems, but they all have different sources. Thanks in advance!

解决方案

The command line for g++ needs to include both source files, like this:

g++ -g -Wall main.cpp translator.cpp -o Pear

Otherwise, the compiler has no idea from where to get the implementation of the translator::translator(std::istream&) member function.

*EDIT: * (from the comment)

I thought that basically the use of header files was so that it would know where to get each implementation of the file?

This part is grossly oversimplified, but it should help you get the picture. Recall that the process of producing an executable from C++ sources consists of two major steps - compilation and linking. The g++ program performs them both (it can do just one if you specify -c flags, or pass only .o files).

The compiler and the linker stages of g++ do not "talk" to each other directly. The compiler produces the inputs for the linker, and that's where the communication ends.

Header files are for the compiler. Specifically, they are for the first stage of compilation - the preprocessing. Once the preprocessor has finished, there is no knowledge of where the definitions came from. Even the compiler does not know it, let alone the linker. That is why you need to "help" the linker by supplying all the relevant sources to g++.

这篇关于未定义的参考编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆