Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template Linking Error

Started by lukar Oct 10, 2004 at 9:11 PM 5 replies 500+ views
Original Post
lukar
lukar
Hi, im making a linked list using a template node but im getting this weird undefined reference to the constructors when using my TNodo class in another class as an int. TNodo seems to be the one causing problems all around... anyone help?

#include "TListaDoble.h"
#include "TNodo.h"
#include <iostream>

TListaDoble::TListaDoble()
{
	Inicio = NULL;
	Actual = Inicio;
}
bool TListaDoble::Vacio()
{
	if(Inicio == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void TListaDoble::InsertarInicio(int var)
{
	Inicio = new TNodo<int>(var);
	Actual = Inicio;
}
void TListaDoble::InsertarFinal(int var)
{
	
	if(Vacio())
	{
		Inicio = new TNodo<int>(var);
	}
	else
	{
		TNodo<int>* temp = new TNodo<int>(var);
		TNodo<int>* aux = Inicio;
	
		while(aux->siguiente != 0)
		{
			aux = aux->siguiente;
		}
		*aux->siguiente = *temp;
		aux->siguiente->anterior = aux;
	}

}
void TListaDoble::InsertarOrdenado(int var)
{
	if(Vacio())
	{
		Inicio = new TNodo<int>(var);
		Actual = Inicio;
	}
	else
	{
		TNodo<int>* aux = Inicio;
		TNodo<int>* temp= new TNodo<int>(var);

		while(aux->info < var)
		{
			aux = aux->siguiente;
		}
		temp->anterior = aux;
		temp->siguiente = aux->siguiente;
		aux->siguiente = new TNodo<int>(var);
		aux->siguiente = temp->siguiente;
		aux->anterior = temp->anterior;
	}	
}

void TListaDoble::Desplegar()
{
	if(Vacio())
	{
		std::cout << "Lista Vacia" << std::endl;
	}
	else
	{
		TNodo<int>* aux = Inicio;
		std::cout << "Lista: ";
		while(aux != 0);
		{
			std::cout << aux->info << " " ;
		}
		std::cout << std::endl;
	}
}

void TListaDoble::SacarInicio()
{
	if(Vacio())
	{
		std::cout << "Lista Vacia" << std::endl;
	}
	else
	{
		Inicio = Inicio->siguiente;
		Actual = Inicio;
	}
}

void TListaDoble::SacarFinal()
{
	if(Vacio())
	{
		std::cout << "Lista Vacia" << std::endl;
	}
	else
	{
		TNodo<int>* aux = Inicio;
		while(aux->siguiente != 0)
		{
			aux = aux->siguiente;
		}
		aux = aux->anterior;
		aux->siguiente = 0;
	}
}

void TListaDoble::SacarOrdenado(int var)
{
	if(Vacio())
	{
		std::cout << "Lista Vacia" << std::endl;
	}
	else
	{
		TNodo<int>* aux = Inicio;

		while(aux->info != var | aux !=0)
		{
			aux = aux->siguiente;
		}
		if(aux = 0){ std::cout << "No existe numero " << var << " en la lista" << std:: endl;
		}
		aux->anterior->siguiente = aux->siguiente;
		
		
	}		
}

and

#ifndef H_TLISTADOBLE
#define H_TLISTADOBLE
#include "TNodo.h"

class TListaDoble
{
public:
	TListaDoble();
	bool Vacio();
	void InsertarInicio(int);
	void InsertarFinal(int);
	void InsertarOrdenado(int);
	void Desplegar();
	void SacarInicio();
	void SacarFinal();
	void SacarOrdenado(int);

	TNodo<int> *Inicio;
	TNodo<int> *Actual;
	
};

I have neurons in my butt!!!
Evil Steve
Evil Steve
The defintion of TNode might help :P
Are all the TNode member functions inline? Whats the exaxct linker error? And is the problem only with the constructor, or are there any other problems?
lukar
lukar
sorry hehehe

#include "TNodo.h"template <class T>TNodo<T>::TNodo(){	siguiente = NULL;	anterior = NULL;}template <class T>TNodo<T>::TNodo(T param){	info = param;	siguiente = NULL;	anterior = NULL;}template <class T>TNodo<T>::TNodo(const TNodo<T> &param){	*this = param;}template <class T>TNodo<T>& TNodo<T>::operator=(const TNodo<T> param){	siguiente = param.siguiente;	anterior = param.anterior;	info = param.info;	return *this;}


and

#ifndef H_TNODO#define H_TNODO//Clase Template TNodotemplate <class T>class TNodo{	public:	TNodo(); 			//Constructor en nulos	TNodo(T);			//Constructor con templates	TNodo(const TNodo<T> &);			//Constructor con otro nodo	TNodo<T>& operator=(const TNodo<T>); 	//Operador igualador de nodos	T info;	TNodo *siguiente; 		//Apuntador al nodo siguiente	TNodo *anterior; 		//Apuntador al nodo anterior	};#endif


THe exact linking error is TListaDoble.o(.text+0xbd):TListaDoble.cpp: undefined reference to TNodo::TNodo(int)

and another one

TListaDoble.o(.text+0x2ed):TListaDoble.cpp: undefined reference to TNodo::TNodo(TNodo const&)

and

TListaDoble.o(.text+0x2ff):TListaDoble.cpp: undefined reference to TNodo::operator=(TNodo)
I have neurons in my butt!!!
Evil Steve
Evil Steve
I'm not entirely sure. Try making your template functions inline (in the header), and see if that fixes it. Some compilers have problems with non-inline template functions.
lukar
lukar
could you give me an example for that?
I have neurons in my butt!!!
Evil Steve
Evil Steve
Sure, in your header:
#ifndef H_TNODO#define H_TNODO//Clase Template TNodotemplate <class T>class TNodo{public:   TNodo()   {      siguiente = NULL;      anterior = NULL;   }   TNodo(const T& param)   {      info = param;      siguiente = NULL;      anterior = NULL;   }   TNodo(const TNodo<T> & param)   {      *this = param;   }   TNodo<T>& operator=(const TNodo<T>& param)   {      siguiente = param.siguiente;      anterior = param.anterior;      info = param.info;      return *this;   }   T info;   TNodo *siguiente; 		//Apuntador al nodo siguiente   TNodo *anterior; 		//Apuntador al nodo anterior};#endif

and you can remove your source file

EDIT: Changed the source to be copy and paste-able
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Evil Steve
I'm not entirely sure. Try making your template functions inline (in the header), and see if that fixes it. Some compilers have problems with non-inline template functions.

It's not that "some compilers have problems with non-inline template functions," it's that template definitions just always have to be made visible to every translation unit that use them (unless you use export). This is completely standard functionality. If you put the definition in a cpp file without export and try to call the function from a separate cpp file, it should always fail. This doesn't mean the templates have to be inline, but the definitions do have to be visible from whichever translation unit you use them in, so put them in a file included in whatever translation units use them but don't necessarily explicitly mark them as inline (unlike with non-template functions, this will not cause multiple definition errors).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.