G++ 为定义的函数抛出大量'undefined reference'
g++ throwing tons of 'undefined reference' for defined functions
当我把链表编程为家庭作业时,一切都很好,直到我尝试实现最后一个方法,即计算元素数量的方法。在编写和编译之后,我使用的链表类的每一个方法现在都在抱怨未定义的引用。
所以我撤消了所有操作,直到创建count方法之前,它编译得很好。"我以后会考虑的",我告诉自己,"让我吃一片披萨吧。"哦,我错了。回来后,我试着重新编译,只是为了确定,因为我担心,每个方法都会出现相同的undefined reference to LinkedList<type>::anything()
错误!不更改代码的任何字符!
这是代码,抛出了大量相同的错误:检查Github(main在Trabalho05.cpp
上,它是葡萄牙语,但与列表相关的东西不是)。我得到以下错误:
$ g++ Trabalho05.cpp LinkedList.cpp Lancamento.cpp Node.cpp
/tmp/cc2rL0ax.o: In function `listarTran()':
Trabalho05.cpp:(.text+0x14): undefined reference to `LinkedList<Lancamento>::empty()'
Trabalho05.cpp:(.text+0x3d): undefined reference to `LinkedList<Lancamento>::begin()'
Trabalho05.cpp:(.text+0x4f): undefined reference to `LinkedList<Lancamento>::iterator::operator->()'
Trabalho05.cpp:(.text+0x6c): undefined reference to `LinkedList<Lancamento>::iterator::operator->()'
Trabalho05.cpp:(.text+0xa4): undefined reference to `LinkedList<Lancamento>::iterator::operator++()'
Trabalho05.cpp:(.text+0xb3): undefined reference to `LinkedList<Lancamento>::end()'
Trabalho05.cpp:(.text+0xca): undefined reference to `LinkedList<Lancamento>::iterator::operator!=(LinkedList<Lancamento>::iterator const&)'
/tmp/cc2rL0ax.o: In function `remTran()':
Trabalho05.cpp:(.text+0x198): undefined reference to `LinkedList<Lancamento>::destroy()'
Trabalho05.cpp:(.text+0x24c): undefined reference to `LinkedList<Lancamento>::remove(unsigned int)'
Trabalho05.cpp:(.text+0x320): undefined reference to `LinkedList<Lancamento>::count()'
/tmp/cc2rL0ax.o: In function `lancarTran()':
Trabalho05.cpp:(.text+0x531): undefined reference to `LinkedList<Lancamento>::push(Lancamento const&)'
/tmp/cc2rL0ax.o: In function `mostraSaldo()':
Trabalho05.cpp:(.text+0x62e): undefined reference to `LinkedList<Lancamento>::begin()'
Trabalho05.cpp:(.text+0x640): undefined reference to `LinkedList<Lancamento>::iterator::operator->()'
Trabalho05.cpp:(.text+0x662): undefined reference to `LinkedList<Lancamento>::iterator::operator++()'
Trabalho05.cpp:(.text+0x66c): undefined reference to `LinkedList<Lancamento>::end()'
Trabalho05.cpp:(.text+0x683): undefined reference to `LinkedList<Lancamento>::iterator::operator!=(LinkedList<Lancamento>::iterator const&)'
Trabalho05.cpp:(.text+0x691): undefined reference to `LinkedList<Lancamento>::begin()'
Trabalho05.cpp:(.text+0x6a3): undefined reference to `LinkedList<Lancamento>::iterator::operator->()'
Trabalho05.cpp:(.text+0x6c5): undefined reference to `LinkedList<Lancamento>::iterator::operator++()'
Trabalho05.cpp:(.text+0x6cf): undefined reference to `LinkedList<Lancamento>::end()'
Trabalho05.cpp:(.text+0x6e6): undefined reference to `LinkedList<Lancamento>::iterator::operator!=(LinkedList<Lancamento>::iterator const&)'
/tmp/cc2rL0ax.o: In function `__static_initialization_and_destruction_0(int, int)':
Trabalho05.cpp:(.text+0x90e): undefined reference to `LinkedList<Lancamento>::LinkedList()'
Trabalho05.cpp:(.text+0x91d): undefined reference to `LinkedList<Lancamento>::~LinkedList()'
Trabalho05.cpp:(.text+0x92c): undefined reference to `LinkedList<Lancamento>::LinkedList()'
Trabalho05.cpp:(.text+0x93b): undefined reference to `LinkedList<Lancamento>::~LinkedList()'
collect2: error: ld returned 1 exit status
我以前从未遇到过同样的问题,经过仔细搜索,我没有发现整个类都是undefined reference
d的类似问题。
问题是它试图实例化类型为Lancamento
的模板,但在LinkedList.cpp中找不到代码。在某个阶段,编译器需要在同一文件中包含以下所有(技术上:相同的翻译单元):
Lancamento
的定义LinkedList
的完整定义(包括方法)- 参考
LinkedList<Lancamento>
哪里由你决定。使用模板时,最好将所有模板代码放在一个标头中,如果需要,请使用inline
方法。查看此处了解更多信息。
此外,我强烈建议使用std::list
。标准图书馆是你的朋友,会做得比你更好。
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 安卓 NDK 制造。数百"undefined reference error"
- 当我尝试进行TPC-E测试时,实用程序抱怨"undefined reference"
- 声明和定义函数静态会产生"undefined reference to function_name()"
- C++ : "undefined reference to"可能是因为CMake文件
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- AppData\Local\Temp\cc59LXDc.o:test1.cpp: undefined reference to 'constructNewObjectOfClass
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"