Frage

While I was programming a linked list as homework, everything was fine until I tried to implement the last method, the one to count the number of elements. After writing it and compiling, every method of the linked list class I was using was now complaining about undefined reference.

So I undone everything until just before creating the count method, and it compiled fine. "I will think about it later", I told myself, "let me just eat a slice of pizza.". Oh, how wrong was I. After I came back, I tried to recompile just to be sure and, for my fear, I got the same error of undefined reference to LinkedList<type>::anything() for every method! Without changing a character of code!

This is the code, throwing a ton of the same error: check on Github (the main is on Trabalho05.cpp and it's in portuguese, but list-related things aren't). I get the following errors:

$ 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

I have never had this same issue before, and after thorougly searching, I did not found similar problems of an entire class being undefined referenced.

War es hilfreich?

Lösung

The issue is that it's trying to instantiate the template with the type Lancamento and it can't find the code in LinkedList.cpp. At some stage the compiler needs all of the following in the same file (technically: same translation unit):

  • the definition of Lancamento
  • the full definition of LinkedList (including methods)
  • a reference to LinkedList<Lancamento>

Where is up to you. When using templates it's best to put all the template code in a header together, using inline if need be on methods. Check here for further reading.

Also, I strongly recommend to use std::list. The standard library is your friend and will do things better than you can.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top