Template Vector class

Hi everyone!

I am having problem with my destructor;


private:
T list[100];
int size;
};

template<class T>
myVector<T>::~myVector()
{
delete [] list; //problem deleting the list...
}

what may be the cause??
Only delete[] what you new[]
In this case you don't need to code the destructor.
you can use as
1
2
3
4
5
6
7
8
9
template<class T>
class A
{
   private:
           T*   list; 
   public:
           A<T>() { new T[100] ; } 
           ~A<T>() { delete [] T; }  
};
Thanks ne555
@bluecoder:

I am a novice programmer and just started to learn it. And yet I havnt finished all parts of programming. What I am going to do is just to write my own template vector class which then works for push, start, end, size, etc like simple functions..I have written the implementation of this function but the program is not running well. The main problem was with this destructor. Let me know if you can provide me the simple implementations.... Thanks
ne555 was just saying you don't need the delete[] call in your destructor.

bluecoder was just showing you how you could write your class such that you would need the delete[] call in your destructor.

The "main problem" wasn't with your destructor, it was in you memory allocation/deallocation scheme.

If you do not uses new[], you must not use delete[].
If you want to use delete[], you must use new[].

There are valid reasons to do it either way. Usually, though, if possible, you would want to write your class without the use of new[] and delete[].
@doug4: many thanks for your clarification... HOpe that works...

But can you pls help me to write some sample vector on my earlier forum pls... or can I have any links to the sample vector classes to refer??
Just take your header file and get rid of the destructor. Here is what I came up with based on your code snippet and your post 3 posts back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
class vector
{
public:
    vector() : size(0) {}  // initialize the size in the constructor

    void push(const T& value);

    /* Other member functions here */

private:
    T list[100];
    int size;
};


@doug4

thanks so much...

finally i have the template vector class working now..... Now taking a long breathe..
Topic archived. No new replies allowed.