Click here to Skip to main content
15,886,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem with defining a template class in a header file with namespace, sorry for not being specified in describing the problem in the title but I don't now anything about it, It is my fist template class by the way.
C++
#ifndef MYVECTOR_H_INCLUDED
#define MYVECTOR_H_INCLUDED

namespace sam
{
    template <typename T> class MyVector
    {
    public :
        MyVector();
        MyVector(int size);
        MyVector(MyVector& other);
        T get(int index);
        int size();
        void set(int index,T item);
        void push_back(T item);
        void push_front(T item);
        MyVector& operator= (const MyVector& other);
        ~MyVector();
    private :
        T growArray();
        T * p_array;
        int array_size;
    };
template <typename T> MyVector<T>::~MyVector()
{
    delete p_array;
}

template <typename T> int MyVector<T>::size()
{
    return array_size;
}

template <typename T> MyVector<T>::MyVector(MyVector& other)
{
    this->array_size = other.array_size;
    this->p_array = new int[array_size];
    for (int i = 0; i < array_size; i++)
    {
        p_array[i] = other.p_array[i];
    }
}

template <typename T> MyVector& MyVector<T>::operator=(const MyVector& other)
//invalid use of template-name 'sam::MyVector' without an argument list|
{
    if (this == &other)
    {
        return *this;
    }
    delete p_array;
    this->p_array = new T[other.array_size];
    this->array_size = other.array_size;
    for (int i = 0; i < other.array_size; i++)
    {
        this->p_array[i] = other.p_array[i];
    }
    return *this;
}
template <typename T> void MyVector<T>::push_front (T item)
{
    if (p_array[0] == NULL)
    {
        p_array[0] = item;
    }
    else
    {
        T * p_temp = new T[array_size + 1];
        for (int i = 1; i <= array_size; i++)
        {
            p_temp[i] = p_array[i-1];
        }
        growArray();
        p_array = p_temp;
    }
}

template <typename T> void MyVector<T>::push_back(T item)
{
    if (p_array[array_size-1] == NULL)
    {
        p_array[array_size-1] = item;
    }
    else
    {
        growArray();
        p_array[array_size] = item;
    }
}

template <typename T> MyVector<T>::MyVector()
:array_size(32)
{
    p_array = new int[32];
    for (int i = 0; i < 32; i++)
    {
        p_array[i] = NULL;
    }
}

template <typename T> MyVector<T>::MyVector(int size)
:array_size(size)
{
    p_array = new int[size];
    for (int i = 0; i < size; i++)
    {
        p_array[i] = NULL;
    }
}

template <typename T> T MyVector<T>::get(int index)
{
    if (index >= 0 && index < array_size)
    {
        if (p_array[index] != NULL)
        {
            return p_array[index];
        }
        return 0;
    }
    else
    {
        return 0;
    }
}

template <typename T> T MyVector<T>::growArray()
{
    T* p_temp = new T[array_size + 1];
    p_temp = p_array;
    array_size++;
    delete p_array;
    p_array = p_temp;
}

template <typename T> void MyVector<T>::set(int index,T item)
{
    if (index >= 0 && index < array_size)
    {
        p_array[index] = item;
    }
    else if (index > array_size)
    {
        growArray();
        p_array[array_size] = item;
    }
    else
    {
        return;
    }
}
}

#endif // MYVECTOR_H_INCLUDED

I commented the error message, Please Help.

Thanks, Samuel.
Posted
Updated 22-Jun-15 3:23am
v3
Comments
Philippe Mori 22-Jun-15 12:54pm    
By the way, you must call delete[] for any pointer allocated with new[]. Otherwise, you will have memory leaks.
Stefan_Lang 23-Jun-15 3:13am    
Not the error you asked for, but on several occasions you allocate p_array as new int[] rather than new T[]. You should fix that as well.

Your return value must also have a template specification.

template <typename T> MyVector<T> & MyVector<T>::operator=(const MyVector& other)
 
Share this answer
 
Comments
CPallini 22-Jun-15 11:31am    
Exactly, 5.
Albert Holguin 22-Jun-15 15:53pm    
+5
Sergey Alexandrovich Kryukov 22-Jun-15 16:29pm    
Sure, a 5.
—SA
Samuel Shokry 24-Jun-15 8:54am    
Thanks That's great
In addition to the fix to your error (see solution 1), there are several errors in the code, some of them severe:

1. on several occasions you allocate your internal array calling new int[] rather than new T[]

2. your method grow_array() has some severe issues:
C++
T* p_temp = new T[array_size + 1];//ok
p_temp = p_array;//leak error 1
array_size++;//ok
delete p_array;//runtime error 2
p_array = p_temp;//fatal error 3
// error 4

- error 1: you just lost your only reference to the newly allocated array - memory leak! Solution: just remove that line!
- error 2: you must use delete[] p_array, else you get a runtime error. There's also the secondary issue that you're losing all the values currently stored in p_array. Didn't you want to copy them to the new array?
- error 3: since p_temp is pointing to the same address p_array is pointing to, this line does nothing at all. However, it does mean that p_array is (still) pointing to invalidated memory, and the next attempt to use it will result in a fatal error that crashes the program! Solution: will be fixed after fixing error 1.
- error 4: the return statement is missing (but then, you don't appear to be using a return value, so you might just as well change the return type to void)

3. your method push_front() calls grow_array, but also redundandly does the required allocation itself.

4. The methods push_front and push_back query values of p_array without checking if it is even defined: if p_array is 0, then this may result in a crash.

5. the method set uses grow_array in case the index is out of range, but that won't be sufficient if the index is out of range by more than 1. Consequently, the value may be stored at an unexpected location!

And one minor issue:
Your default constructor allocates an array of default length and then initializes all array elements with NULL. You should be using 0 here - or better:
p_array[i] = T(0);
 
Share this answer
 
v4
Comments
CPallini 23-Jun-15 4:47am    
5. Good points.
nv3 23-Jun-15 4:53am    
You did a whole code review. 5.
Samuel Shokry 24-Jun-15 8:54am    
WOW !! I thought I had a few problems now I have a lot of them, Thank you very much you helped me a lot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900