hello
I'm learning c++ know, and i'm on the class templates subject
I allways make the main program in a main.cpp file
the class definition on aseparate file ie: myClass.h
the function definitions in other file ie: myFuncs.cpp
i compile and never get any error
know that im learning class templates cant work that way
if i use 3 files the compiler(vc++6.0) displays this error:
"undefined reference to myClass<double>objectDouble(5)"
in the main code.
but when i put in the same file the class definition and the methods my program compiles just fine.
ok i must put in the same file class def and func def when working with class templates?? and why??, if not what i am doing wrong?
heres the code if u need it
Code:
----------------------------------------------------------------------------------
"file main.cpp"
#include <iostream>
using namespace std;
#include "arr_burbuja.h"
int main()
{
int arregloInt[5]={4,7,6,3,5};
double arregloDoble[5]={3.6,2.5,9.6,4.2,7.3};
Arreglo<int>tipoInt(5);
Arreglo<double>tipoDoble(5);
tipoInt.ordenar(arregloInt);
tipoInt.imprimir();
cout << endl;
tipoDoble.ordenar(arregloDoble);
tipoDoble.imprimir();
cout << endl;
return 0;
}
---------------------------------------------------------------------------------
file "arr_burbuja.h"
#ifndef ARR_BURBUHA_H
#define ARR_BURBUHA_H
template <class T>
class Arreglo{
public:
Arreglo(int = 0);
void ordenar(T *);
void imprimir()const;
private:
int tamanio;
T *ptr;
};
----------------------------------------------------------------------------------
file "arr_burbuja.cpp"
#include <iostream>
using std::cout;
#include "arr_burbuja.h"
//definicion de funciones miembro de la clase burbuja
//constructor
template <class T>
Arreglo<T>::Arreglo(int tArreglo)
:tamanio(tArreglo)
{
ptr = 0;
}
//ordena el arreglo
template <class T>
void Arreglo<T>::ordenar(T *ptrArreglo)
{
T temporal;
for(int i = 0; i < tamanio-1;i++){
for(int j = 0; j < tamanio-1;j++){
if(ptrArreglo[j] > ptrArreglo[j+1]){
temporal = ptrArreglo[j];
ptrArreglo[j] = ptrArreglo[j+1];
ptrArreglo[j+1] = temporal;
}
}
}
ptr = ptrArreglo;//para utilizar ptr en la clase
}
//imprime el arreglo
template <class T>
void Arreglo<T>::imprimir()const
{
for(int i = 0; i < tamanio; i++)
cout << ptr[i] << ' ';
}
please excuse my poor english!