Я пытаюсь создать шаблон класса с именем Vector, который используется для создания динамических c массивов любого типа. Когда вызывается мой конструктор по умолчанию, он создает новый массив и вызывает метод initialize, который инициализирует все значения массива dynamici c нулями, используя nullptr. Почему-то при компиляции я получаю ошибку: 'nullptr' was not declared in this scope
в этой строке. В чем может быть проблема?
Также, любые отзывы о моем классе будут хорошими. Я еще не проверял это из-за этой ошибки, но я надеюсь, что он сможет успешно обрабатывать любой тип массива, особенно массивы объектов.
Vector.h:
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
class Vector
{
public:
Vector(int size = 10);
~Vector();
void initialize(int from);
void expand();
void push(const T &element);
int size(){return this->nrofel;}
T& operator[](const int index);
private:
T **data;
int capacity;
int nrofel;
};
template <class T>
Vector<T>::Vector(int size){
this->capacity = size;
this->nrofel = 0;
this->data = new T*[this->capacity];
initialize(this->nrofel);
}
template <class T>
T& Vector<T>::operator[](const int index){
if(index < 0 || index >= this->nrofel){
throw("Out of bounds.");
}
return *this->data[index];
}
template <class T>
void Vector<T>::initialize(int from){
for(size_t i = from; i < capacity; i++){
this->data[i] = nullptr;
}
}
template <class T>
Vector<T>::~Vector(){
for(size_t i = 0; i < capacity; i++){
delete this->data[i];
}
delete[]this->data;
}
template <class T>
void Vector<T>::expand(){
this->capacity *= 2;
T ** tempData = new T*[this->capacity];
for(size_t i = 0; i < this->nrofel; i++){
tempData[i] = this->data[i];
}
this->data = tempData;
initialize(this->nrofel);
}
template <class T>
void Vector<T>::push(const T& element){
if(this->nrofel >= this->capacity){
this->expand();
}
this->data[this->nrofel++] = new T(element);
}
#endif // VECTOR_H