0

I have a header file, but I don't know how to do it: I am new to templates.

template <class T> 
class Array 
{
    public:
        Array(int l, int h);
        Array(int s);
        Array(const Array& other);
        ~Array();
        T& operator[](int index);
        const T& operator[](int index) const;
        int get_size() const 
        {return size;}
    private:
        int low;
        int  high;
        int size;
        int offset;
        T *ptr;  
};

Edited by deceptikon: Moved to C++

2
Contributors
2
Replies
10
Views
4 Years
Discussion Span
Last Post by TuteePink
0

Include the implementation of the class in the header file too. Without getting into excessive detail, template classes and member function definitions aren't split across multiple files.

0

I implemented this one in my main. And, i have to make declerations to each. How do I declare it? I wrote it like this:

template <class T>
T Array<T>::Array (int l, int h)
{
    if (l>h)
    {
        cout<<"Bad number!\n"<<endl;
        exit (1);
    }
    else
    {
        low = l;
        high = h;
        size=(-1*l)+h;
        for (int i=0; i<size; i++)
        {
            ptr[i]=false;
        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.