lundi 7 juin 2021

initialize multidimensional array in class

I am trying to build a matrix class through a multidimensioinal array (hopefully with smart pointers) but for the time being I have:

#ifndef MATRIX_H
#define MATRIX_H

#include <iostream>
#include <memory>

class Matrix
{
    private:
        const unsigned int rowSize_;
        const unsigned int colSize_;
        double* data_;

    public:
        // Constructor
        Matrix(unsigned int, unsigned int, double); // Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). 
        Matrix(unsigned int, unsigned int); // Initializes value to zero
        virtual ~Matrix(){ };


        void print();

};

#endif // MATRIX_H

And the .cpp file:

#include "Matrix.h"

Matrix::Matrix(unsigned int rowSize, unsigned colSize, double value)
:   rowSize_(rowSize),
    colSize_(colSize)
{

    data_ = new double[rowSize_][colSize_];
        
}

However, I am getting an error of the type:

error: array size in new-expression must be constant

How can I initialize the 2D array in this matrix class? Is it possible to allocate space for the array with a smart pointer? (unique_ptr for instance?)

Best regards

Aucun commentaire:

Enregistrer un commentaire