Thread: class template user defined obj

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

    class template user defined obj

    hi
    Im having problems with class templates and user defined objects
    ie:

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    #include "plantpila1.h"
    int main()
    {
      double arregloDouble[5]={1.1,2.2,3.3,4.4,5.5};
      Pila<double>doublePila(arregloDouble,5);
      doublePila.imprimir();
      
      int arregloInt[3]={1,2,3};
      Pila<int>intPila(arregloInt,3);
      intPila.imprimir();
        
      //user defined object
      Marciano arregloMarciano[3]={Marciano(" marte "),Marciano("  venus "),Marciano(" jupiter ")};
      Pila<Marciano>marcianoPila(arregloMarciano,3);
      marcianoPila.imprimir();
      
      cout << endl;
      system("PAUSE");	
      return 0;
    }
    
    //heres the class definition
    
    #ifndef PLANTPILA1_H
    #define PLANTPILA1_H
    
    template<class T>
    class Pila{
         public:
              Pila(T *,int = 10);//10 is Pila size
                       
              void imprimir();
              
         private:
              T *ptr;
              int tamanioPila;     
    };
    
    
    //member funcions for Pila
    
    //constructor
    template<class T>
    Pila<T>::Pila(T *arr,int s)
    {
         tamanioPila = s > 0? s : 10;
         ptr = arr;
    }
    
    //print the array
    template <class T>
    void Pila<T>::imprimir()
    {
         for(int i = 0; i < tamanioPila; i++) 
            cout << ptr[i] << ' ';
    }

    ok in the book that im studing says that when you use not native objects(int,float,etc) you need to do that:

    template<>
    class Pila<Marciano>{
    //class body
    };


    but he dont says where or how i use this . how use this syntaxis?
    plase can somebody help me?
    i need this template can work with user defines Marciano.

    please excuse my poor english
    if you need to see Marciano class i can paste it later

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    //print the array
    template <class T>
    void Pila<T>::imprimir()
    {
         for(int i = 0; i < tamanioPila; i++) 
            cout << ptr[i] << ' ';
    }
    Did you overload the insertion operator (<<) in the Marciano class to allow this?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    not yet
    i know i should do that , but thats not the problem , if you see the post, what i dont understand is the syntaxis

    will try to translate what the book says:
    "the programmer can take control and create a hinstance of the Pila class of a specific type, like Marcian. know a new class with the name Pila<Marciano> is created
    template<>
    class Pila<Marciano>{
    };

    this is part is where and how i dont understand how to us it


    or in other words, how the compiler knows how to create a Marciano copy?, the compiler creates a float,int, etc copy of the code but how knows the marciano code?

    sorry if i cant explain it clearly but cant find the words in english to exepress that

    thanks for your post

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I'm not sure I completely understand your question, but I believe this should cover it.

    It will look at your templated class, and know how to build it. It just doesn't know what kind of data it will be handling. To figure that out, it takes a look at the Marciano class you made, and makes sure that everything adds up. If it does, you'll be able to create the Pila class of type Marciano.

    Like JaWiB pointed out, it won't let you use the imprimir function with the Merciano clasunless you have overloaded the << operator for the it.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    ok in other words, i dont have to take care of that because the compiler handle that problem, i just have to overload the << operator.
    am i right?
    ok i got it,
    thanks a lot for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. user defined template
    By xagiber in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 06:34 AM