Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/12/2010, 04:33
bdragonsb3
 
Fecha de Ingreso: diciembre-2010
Mensajes: 4
Antigüedad: 13 años, 4 meses
Puntos: 0
De acuerdo Una Lista Enlazada con Plantillas en C++(duda)

Hola a todos!

Tengo una duda acerca de las templates en C++.
Despues de leer varios tutoriales, por fin pude crear por mi cuenta una Lista Simplemente Enlazada utilizando plantillas y dos clases(Nodo y Lista).

Mi pregunta es:
¿Por que cuando tengo todo mi código junto en un solo archivo, cuando compilo y corro mi programa funciona perfecto, pero cuando separo el codigo en archivos de cabecera(.h y .cpp) mas el main.cpp mi compilador(g++) al momento del enlazado me tira varios errores?

Mi codigo TODO JUNTO el que si funciona es:
Código:
#include <iostream>
using namespace std;

template <class Q>
class Nodo{
	private:
		Q *MiDato;
		Nodo<Q> *next;
	public:
		Nodo(Q nuevo){
			next = NULL;
			MiDato = new Q;
			*MiDato = nuevo;
		}
		Nodo(void): MiDato(NULL), next(NULL) {}
		~Nodo(void){
			delete MiDato;
			next = NULL;
		}
		void set_data(Q nuevo){
			MiDato = new Q;
			*MiDato = nuevo;
		}
		void set_next(Nodo<Q> *s) { next = s; }
		const Q get_data(void) const { return *MiDato; }
		Nodo<Q> *get_next(void) { return next; }
		void ShowAll(void) const{
			Nodo<Q> *aux = next;

			cout << *MiDato << endl;
			while(aux){
				cout << aux->get_data() << endl;
				aux = aux->get_next();
			}
		}
};

template <class Q>
class Lista{
	private:
		Nodo<Q> *inicio;
		Nodo<Q> *final;
		int tam;
	public:
		Lista(void): inicio(NULL), final(NULL), tam(0) {}
		~Lista(void){
			Nodo<Q> *aux = inicio;

			if(!empty()){
				while(inicio){
					inicio = inicio->get_next();
					delete aux;
					aux = inicio;
				}
			}
			final = NULL;
			tam = 0;
		}
		void add(Q nuevo){
			Nodo<Q> *aux = new Nodo<Q>(nuevo);
			if(empty()){
				inicio = aux;
			}else{
				final->set_next(aux);
			}
			final = aux;
			tam++;
		}
		int get_tam(void) const { return tam; }
		bool empty(void) const{
			return (inicio == NULL);
		}
		void Mostrar(void) const { inicio->ShowAll(); }
};

int main(void){
	Lista<int> Buena;
	Lista<char> Mala;

	for(int i = 10; i < 21; i++){
		Buena.add(i);
	}
	Mala.add('M');
	Mala.add('A');
	Mala.add('L');
	Mala.add('A');

	cout << "Buena mide: " << Buena.get_tam() << endl;
	Buena.Mostrar();
	cout << "\nMala mide: " << Mala.get_tam() << endl;
	Mala.Mostrar();

	return 0;
}
Y el error que me tira si creo los siguientes 5 archivos:
main.cpp, Nodo.cpp, Nodo.h, Lista.cpp y Lista.h
es:
Código:
$ g++ -c Nodo.cpp 
$ g++ -c Lista.cpp 
$ g++ main.cpp *.o
/tmp/ccNbYLWk.o: In function `main':
main.cpp:(.text+0x13): undefined reference to `Lista<int>::Lista()'
main.cpp:(.text+0x1f): undefined reference to `Lista<char>::Lista()'
main.cpp:(.text+0x39): undefined reference to `Lista<int>::add(int)'
main.cpp:(.text+0x59): undefined reference to `Lista<char>::add(char)'
main.cpp:(.text+0x6a): undefined reference to `Lista<char>::add(char)'
main.cpp:(.text+0x7b): undefined reference to `Lista<char>::add(char)'
main.cpp:(.text+0x8c): undefined reference to `Lista<char>::add(char)'
main.cpp:(.text+0x98): undefined reference to `Lista<int>::get_tam() const'
main.cpp:(.text+0xcc): undefined reference to `Lista<int>::Mostrar() const'
main.cpp:(.text+0xd8): undefined reference to `Lista<char>::get_tam() const'
main.cpp:(.text+0x10c): undefined reference to `Lista<char>::Mostrar() const'
main.cpp:(.text+0x11d): undefined reference to `Lista<char>::~Lista()'
main.cpp:(.text+0x130): undefined reference to `Lista<char>::~Lista()'
main.cpp:(.text+0x144): undefined reference to `Lista<int>::~Lista()'
main.cpp:(.text+0x160): undefined reference to `Lista<int>::~Lista()'
collect2: ld devolvió el estado de salida 1
$
¿Alguien me puede explicar que sucede?