> elements = (T *)realloc(elements,vectorSize * sizeof(T)); // PROBLEM!
What are the possible problems?
If the type T is not trivially constructible, assignable and destructible, you are in deep trouble - I presume that is not the case.
If realloc happens to return a nullptr, then too you are in trouble - I presume that too is not the case.
If
vectorSize
is negative, then too you are in trouble - that is a possibility.
From the code that you have posted, the error appears to be that the pointer
elements
is uninitiazed. Your constructor needs to look something like:
1 2 3 4 5 6
|
template<typename T>
MyVector<T>::MyVector()
{
elements = nullptr ; // C++98: elements = 0 ;
vectorSize = 0;
}
|
You also need to write a copy constructyor, a destructor and an overloaded assignment operator.
And instantiate
MyVector<>
with only pod types. For example,
1 2
|
MyVector<int> vec_one ; // ok
//MyVector<std::string> vec_two ; // not ok
|