Thread: Templates and Templates objects

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    Templates and Templates objects

    considering this small code:
    here a template array class is written and also a cat class.
    the cat class has "present" method.
    the array is initialized with 20 cats. yet, trying to activate a cat methods makes a compilation error:
    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T>
    class Array{
        private:
            T* array;
            int size;
        
        public:
            Array();
            Array(int _size);
            Array (Array& rhs);
            ~Array();
            
            T& operator [] (int offset);
    };
    
    template <class T>
    Array<T>::Array():size(0), array(NULL) {};
    
    template <class T>
    Array<T>::Array(int _size): size(_size){
        array=new T[size];
    }
    
    template <class T>
    Array<T>::Array (Array& rhs){
        size=rhs.size;
        array=new T[size];
        
        for (int i=0;i<size;i++){
            array[i]=rhs[i];    
        }
    }
    
    template <class T>
    Array <T>::~Array(){
        delete[] array;
    } 
    
    template <class T>
    T& Array<T>::operator [] (int offset){
        T t=array[offset];
        return t;
    }
    
    class Cat{
        public:
            void meaw (void){
                cout<<"Mieaaw\n";
            }
            
            void present (void){
                cout<<"furrColor: "<<furrColor<<" age: "<<age<<"\n";
            }
            
            string furrColor;
            int age;
            Cat (): furrColor("Black"), age(1){};
    };
    
    
    int main (void){
        
        Array<Cat> array[20] ;
        
        for (int i=0;i<20;i++){
            array[i].present();
        }
        
        return 0;
    }
    error:
    [Error] 'class Array<Cat>' has no member named 'present'

    how do we make it work? I'm sure there's a way I'm not aware of since if we can't - what is the point of templates?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T>
    class Array{
        private:
            T* array;
            int size;
        
        public:
            Array();
            Array(int _size);
            Array (Array& rhs);
            ~Array();
            
            T& operator [] (int offset);
    };
    
    template <class T>
    Array<T>::Array():size(0), array(NULL) {};
    
    template <class T>
    Array<T>::Array(int _size): size(_size){
        array=new T[size];
    }
    
    template <class T>
    Array<T>::Array (Array& rhs){
        size=rhs.size;
        array=new T[size];
        
        for (int i=0;i<size;i++){
            array[i]=rhs[i];    
        }
    }
    
    template <class T>
    Array <T>::~Array(){
        delete[] array;
    } 
    
    template <class T>
    T& Array<T>::operator [] (int offset){
    //     T t=array[offset];
    //     return t;
        return array[offset];
    }
    
    class Cat{
        public:
            void meaw (void){
                cout<<"Mieaaw\n";
            }
            
            void present (void){
                cout<<"furrColor: "<<furrColor<<" age: "<<age<<"\n";
            }
            
            string furrColor;
            int age;
            Cat (): furrColor("Black"), age(1){};
    };
    
    
    int main (void){
        
        Array<Cat> array(20)/*[20]*/ ;
        
        for (int i=0;i<20;i++){
            array[i].present();
        }
        
        return 0;
    }
    First, this
    Code:
    $ g++ bar.cpp
    bar.cpp: In member function ‘T& Array<T>::operator[](int) [with T = Cat]’:
    bar.cpp:70:16:   instantiated from here
    bar.cpp:45:7: warning: reference to local variable ‘t’ returned [enabled by default]
    Note that I return a reference to the actual object, not a reference to a local variable copy of the object.

    > Array<Cat> array(20)/*[20]*/ ;
    See how this creates an array with 20 elements (the array is inside the Array class).

    You can have array[20], but it's a completely different animal, and would need array[x][y] to subscript it!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Line 67 is declaring an array of 20 objects of type Array<cat>. It is not creating an Array<Cat> for which the _size member is equal to 20. Your code is treating it as if it does, hence the compiler complaint. Despite similarity, there is no relationship between "array" (as per meaning of that term in the C or C++ standards) and "Array" (the name of your templated class).


    Also, get out of the habit of having member, argument, or variable names prefixed by an underscore. The standard reserves such identifiers for use (in a number of circumstances) by the implementation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    I feel so stupid..
    thanks guys!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your copy constructor should take its argument by const reference.
    You should also create an assignment operator and make a const version of the index operator.
    You are also aware that this essentially duplicates std::vector, yes?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    Quote Originally Posted by Elysia View Post
    Your copy constructor should take its argument by const reference.
    You should also create an assignment operator and make a const version of the index operator.
    You are also aware that this essentially duplicates std::vector, yes?
    1) you're absolutely right, I keep forgetting to declare cpy cosnt. as one which gets const ref.. we already talked about it that it should get const ref. in order for it to not change the object copied and also to get temporeries.
    2) this exercise is for studying only. I do aware it's a (bad) replica of <vector> .. thanks

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave11 View Post
    2) this exercise is for studying only. I do aware it's a (bad) replica of <vector> .. thanks
    Okay, that's fine. Just checking.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates...
    By tennisstar in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2012, 10:19 AM
  2. Templates in C++
    By flexo87 in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2011, 01:49 PM
  3. How to use templates on this.
    By m3rk in forum C++ Programming
    Replies: 9
    Last Post: 08-19-2009, 06:15 PM
  4. templates
    By yosef_yaniv in forum C++ Programming
    Replies: 9
    Last Post: 02-04-2008, 12:30 PM
  5. Templates
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2003, 03:58 PM