What is the best option to allocate memory for Matrix?
I need to allocate memory in the first Mat
Constructor .
The second Mat
Constructor needs 1 argument, when Matrix is considered 1D array, so the number of rows will be 1. I also have to allocate memory in this second constructor.
Matrix data should be double
this is what I have tried:
#include<iostream>
class Mat{
uint16_t rows;
uint16_t colls;
double *mData; // the 2D array// matrix
Mat(uint16_t r, uint16_t c){ // constructor with 2 parameters -
// alocating memory for matrix
this-> rows = r;
this-> colls = c;
this-> mData = new double [rows][colls];
}
Mat(uint16_t x){ //constructor with 1 parameter // alocating memory
//for the case when number of rows = 1 , so the matrix becomes 1D array[]
this-> rows = x;
this-> mData = new double [rows];
}
};
I'm not good at pointers, so i can't figure out what I am missing.
"Array size is not a constant expression"
--UPDATE --
Full request: Create the Mat class by implementing its contents as follows:
The class must contain 2 private attributes called mCols andmRows.These are the dimensions of the matrix, and their type is a 16-bit integer without a 16-bit mark.
The class must contain 1 private attribute named mData. This is a pointer to fractional numbers on double precision, and the memory for this pointer will be allocated in the constructor.
A constructor that takes as an argument two unmarked integers that represent the number of lines and the number of columns of an array and allocates memory to store these values.
A second constructor to take as an argument a single number. In this case, the array is considered a vector, the number of lines will be by default 1 and the received argument represents the number of columns. Of course, the allocation of memory to be able to store numbers must also be done in this case.
A third constructor who doesn't take any arguments. It will only initialize the integer attributes with the value of 0.
A fourth constructor, which will be the copy constructor. It receives as an argument an object of type const Mat & and its role is to copy all the values of the attributes of the given object as a parameter in the current object (this).
New code:
class Mat{
private:
uint16_t mRows;
uint16_t mCols;
double *mData;
public:
Mat(uint16_t r, uint16_t c){
mRows = r;
mCols = c;
mData = new double[mRows * mCols];
}
Mat(uint16_t c){
mCols = c;
mRows = 1;
mData = new double [1 * mCols];
}
Mat(){
mRows = 0;
mCols = 0;
}
Mat(const Mat &mat) : mRows(mat.mRows), mCols(mat.mCols), mData(mat.mData){
}
};