0

The solution to the duplicate question did not work

I have the following files:

ListaEnc.hpp

#include "Elemento.hpp"

template<typename T>
class ListaEnc {

public:
    ListaEnc();
    ~ListaEnc();
//  inicio
    void adicionaNoInicio(const T& dado);
    T retiraDoInicio();
    void eliminaDoInicio();
        T pegarHead();
//  posicao
    void adicionaNaPosicao(const T& dado, int pos);
    int posicao(const T& dado) const;
    T* posicaoMem(const T& dado) const;
    bool contem(const T& dado);
    T retiraDaPosicao(int pos);
//  fim
    void adiciona(const T& dado);
    T retira();
//  especifico
    T retiraEspecifico(const T& dado);
    void adicionaEmOrdem(const T& data);
//  outras
    bool listaVazia() const;
    bool igual(T dado1, T dado2);
    bool maior(T dado1, T dado2);
    bool menor(T dado1, T dado2);
    void destroiLista();
        int pegarTamanhoLista();

private: //trocar pra private
    Elemento<T>* head;
    int size;
};

ListaEnc.cpp

#include "ListaEnc.hpp"
#include <cstdlib>
#include <iostream>

template<typename T>
ListaEnc<T>::ListaEnc()
{
     ...
}

main.cpp

#include "ListaEnc.hpp"
using namespace std;
int main(int argc, char** argv)
{
    double x1, y1;
    
    x1 = 2; y1 = 4.2;

    ListaEnc<int>* teste = new ListaEnc<int>();
    
    return 0;
}

This should compile just fine, but I get an undefined reference error to ListaEnc on main.cpp. I have more files in which the reference to ListaEnc is also undefined, but I tried isolating it and still can't get it to work. Does anyone understand why this is happening?

edit: More specifically, there is an undefined error to the constructor and destructor of ListaEnc

Community
  • 1
  • 1
Tuma
  • 603
  • 1
  • 7
  • 35
  • *"This question has been asked before and already has an answer. If those answers do not fully address your question, please **ask a new question**."* Also include an [MCVE](http://stackoverflow.com/help/mcve) as well as the exact error message and, if relevant (you'll find out while crafting the MCVE), the compiler options and version. *"The solution did not work"* is not a useful problem description, making it big and bold does not make it clearer either. – Baum mit Augen Aug 30 '15 at 23:41

1 Answers1

0

You can't separate declaration from implementation when you instantiate a template class. Combine your code to a single file and include that in your main.cpp.

Or, even go a step further and implement the functions inline in your class declaration if that suits your coding style.

Amit
  • 45,440
  • 9
  • 78
  • 110
  • It worked for main.cpp, but it moved to another class – Tuma Aug 30 '15 at 23:34
  • I don't know what you did, or what worked, but what I meant is that you combine ListaEnc.hpp with ListaEnc.cpp. – Amit Aug 30 '15 at 23:37
  • That's what I did, simply pasted ListaEnc.cpp into my .hpp file. The "undefined reference" error is no longer in my main.cpp, but on another class that uses ListaEnc – Tuma Aug 30 '15 at 23:39
  • and did you include your header file there as well? – Amit Aug 30 '15 at 23:44
  • Yeah, I'm writing a new question, but I have a 90 min limit. On Poligono.hpp there is a reference to ListaEnc.hpp, and on Poligono.cpp, a reference to Poligono.hpp – Tuma Aug 30 '15 at 23:54