Jump to content
  • Advertisement
Sign in to follow this  
lukar

Template Linking Error

This topic is 5033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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<int> 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;
	
};

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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 TNodo


template <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<int>::TNodo(int)

and another one

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

and

TListaDoble.o(.text+0x2ff):TListaDoble.cpp: undefined reference to TNodo<int>::operator=(TNodo<int>)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sure, in your header:

#ifndef H_TNODO
#define H_TNODO


//Clase Template TNodo


template <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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!