0

I have template class Matrix and I want to create container to it without stl, but i have a problem with addding matrices to container (same matrices!!!). I create temp container that gets all the data and then delete all my data from container. I create new space for +1 Matrix in container and then try to put everything to my new container:

template<int row, int col, typename T=int>
class MatrixContainer {
public:
MatrixContainer<row, col, T>() {
    size = 0;
    container = new Matrix<row,col,T>[size];
}
void addMatrix(const Matrix<row, col, T> &mat) {
    this->size=this->size+1;
    Matrix<row,col,T> *temp = new Matrix<row,col,T>[this->size-1];
    for (int i = 0; i < size-1; ++i) {
        temp[i] = this->container[i];
    }
    delete[] this->container;
    this->container = new Matrix<row,col,T>[size];
    for (int i = 0; i < size-1; ++i) {
        this->container[i] = temp[i];
    }
    container[size]=mat;
    delete[]temp;
}
private:
Matrix<row, col, T> *container;
int size;
};

Everything compiles but when it sees

container[size]=mat;

it calls to copy constructor and then fails. That's a copy constructor in template class Matrix:

Matrix(const Matrix &other) {
    this->elements = new T *[other.rows];
    for (int i = 0; i < other.rows; i++) {
        this->elements[i] = new T[other.cols];
    }
    this->rows = other.rows;
    this->cols = other.cols;
    for (int i = 0; i < this->rows; ++i) {
        for (int j = 0; j < this->cols; ++j) {
            this->elements[i][j] = other.elements[i][j];
        }
    }
}

I already tried everything but it fails every time when it gets this line

0 Answers0