G++ 为定义的函数抛出大量'undefined reference'

g++ throwing tons of 'undefined reference' for defined functions

本文关键字:reference undefined 定义 函数 G++      更新时间:2023-10-16

当我把链表编程为家庭作业时,一切都很好,直到我尝试实现最后一个方法,即计算元素数量的方法。在编写和编译之后,我使用的链表类的每一个方法现在都在抱怨未定义的引用。

所以我撤消了所有操作,直到创建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。标准图书馆是你的朋友,会做得比你更好。