当我将链接列表编程为作业时,一切都很好,直到我尝试实现最后一个方法,即计算元素数量的方法。在编写和编译之后,我使用的链表类的每个方法现在都在抱怨未定义的引用。
所以我在创建count方法之前撤消了所有内容,并且编译得很好。 “我以后会考虑的,”我告诉自己,“让我吃一片比萨饼。”哦,我错了。我回来后,我试图重新编译,以确保,因为我的恐惧,我得到undefined reference to LinkedList<type>::anything()
每个方法的相同错误!不改变代码的性质!
这是代码,抛出了大量相同的错误:check on Github(主要在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。
答案 0 :(得分:1)
问题在于它尝试使用类型Lancamento
实例化模板,并且无法在LinkedList.cpp中找到代码。在某个阶段,编译器需要在同一个文件中的所有 (技术上:相同的翻译单元):
Lancamento
LinkedList
的完整定义(包括方法)LinkedList<Lancamento>
你在哪里。使用模板时,最好将所有模板代码放在一个标题中,如果需要在方法上使用inline
。检查 here 以便进一步阅读。
此外,我强烈建议使用std::list
。标准库是你的朋友,会比你做得更好。