Thread: class template

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    class template

    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!

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    maybe typename is used in c?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I think I read somewhere that you cant have template classes definitions and declarations in seperate files. So unless you want to include cpp files you will have to put them in one file im afraid

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>class" and "typname" are the same. But then why do they have 2 names for one thing?

    My suspicion is that it's because struct and class aren't exactly the same in C++ (the only difference is the default member access, but that's a difference), but both can be used in templates. Therefore, to allow use of both, the keyword typename was developed to handle both options.


    >>i must put in the same file class def and func def when working with class templates?? and why??

    Yes, for templated classes, the function definitions must go in the header files with the function declarations. I don't know for sure why this is, but it is.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    ok thanks a lot for the replies

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    From the standard: There is no semantic difference between class and typename in a template parameter.

    However, some programmers use "typename" over "class" to indicate that the template parameter is intended to be some basic type (ie. not a class or struct type).

    Having to put all your template code in a header file has to do with the history of C++ compiler implementations. In other words, this isn't a restriction imposed by the standard.

    The Comeau C++ compiler documentation has a nice discussion on the subject, including how different compilers achieve template instantiation. (You'll need some basic understanding of how compilers and linkers work in order to understand it...)
    http://www.comeaucomputing.com/4.0/d...erman/ati.html

    In the end, you gotta be compatible with your compiler - and for most folks, that means putting everything right there in header.

    If it logically makes sense to seperate the code into a header and implementation file, you can simply #include the implementation file at the bottom of your header file.

    gg

  7. #7
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Thats right, and Visual C++ 6.0 definitely does not support having the implementations in a seperate file.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Templates break a lot of standard rules in C++ -> namely function declarations in headers...function bodies/definitions in module files.

    Borland is the same way. Very confusing and because they break some common rules....I think they need re-designed. They are very powerful...but they have been implemented in a strange fashion in just about every compiler I've seen. I know why this is, but perhaps it could be redesigned to fit more with other classes in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM