New to the forums here. I'm a student taking C++ and need some help with an overloaded method. I've tried looking up everything but confused. Maybe someone here can shed some light on my problem
I have a class that dynamically allocates a 2d array. This array is like a matrix holding doubles. The problem i'm having is with the header file and cpp file associated with the class.
I need to overload the method * and multiply the 2d array. Here is a few parts of it:
My Header file::
1 2 3 4 5 6 7 8 9 10 11 12 13
class Matrix
{
public:
Matrix(); //Default Constructor
//cut out some of my functions that aren't necessary for this problem
Matrix operator*(const Matrix& a1, constdouble vector);
private:
void initialize(); //initializes the matrix to 0;
double **arrayMatrix;
int row;
int col;
};
My CPP code::
1 2 3 4 5 6 7 8 9
Matrix Matrix::operator*(const Matrix& a1, constdouble vector)
{
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
arrayMatrix[i][j] = arrayMatrix[i][j] * vector; //multiply the matrix by vector
}
}
}
I'm getting the following errors:
16 Matrix.h `void Matrix::operator*(const Matrix&, double)' must take either zero or one argument
102 Matrix.cpp `void Matrix::operator*(const Matrix&, double)' must take either zero or one argument
Instead of overloading * in your way Matrix operator*(const Matrix& a1, constdouble vector);
you need to define it following way
Matrix operator*(constdouble vector)
each overload function of type( +, - , * , / , or == and so on ) has the current object this as it first parameter.
And the function must return an object of type Matrix, in most case a temporary object which holds the result
so your code should be like following
1 2 3 4 5 6 7 8 9 10 11 12 13
class Matrix
{
public:
Matrix(); //Default Constructor
Matrix(int rowSize, int colSize);
//cut out some of my functions that aren't necessary for this problem
Matrix operator*( constdouble vector);
private:
void initialize(); //initializes the matrix to 0;
double **arrayMatrix;
int row;
int col;
};
and CPP file
1 2 3 4 5 6 7 8 9 10
Matrix Matrix::operator*(constdouble vector)
{
Matrix tmp( row, col ) ;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
tmp.arrayMatrix[i][j] = arrayMatrix[i][j] * vector; //multiply the matrix by vector
}
}
return tmp;
}
You should also implments copy-constructor Matrix(const Matrix& other ) , assigment operator = operator and destructor ~Matrix()
If your class has any dynamic members, let say it here **arrayMatrix, so you have to implement assigment operator, copy constructor and destructor, otherwise it wont work.
a possible assigment operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Matrix& operator=( const Matrix& other ){
if( this == &other )
return *this;
if( row != other.getRow() || col != other.getCol() ){
clean() ; // it is the destructor, implement it to destroy the current object
row = other.getRow();// you should implement getters
col = other.getCol(); //
arrayMatrix= newdouble*[ row ];
for(int i = 0 ; i < row ; i++){
arrayMatrix[i] = newdouble[ col];
}
// here a copy values
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
arrayMatrix[i][j] = other[i][j]; // [] operator should be implemented as well
return *this;
}
And about the error change the method param to double vector, it should work than
Matrix operator*(double vector)
and it should be this way, the safer way t1 = t1* (double )3;
class Matrix{
//do some thing
};
ostream& operator<<(ostream& out, const Matrix& test){
//do some thing
}
// or depending on your compiler
ostream& operator<<(ostream& out, Matrix const& test){
//do some thing
}
your class must implement the const version of num_rows , num_rows and operator []
like following
1 2 3 4 5 6 7 8 9 10 11
class Matrix{
public:
int num_rows(){
//do some thing
}
int num_rows() const{
// do the same as the normal version of num_rows
}
};
do the same thing for the two other methods too.
you dont have to implement the methods within the class, you can also implement them in your source file....
class Matrix
{
public:
Matrix(); //Default Constructor
Matrix(int row, int col); //Constructor for 2 variables (rows and cols)
Matrix(const Matrix& other); //Copy constructor
~Matrix(); //Default Destructor
int num_rows() const; //returns the number of rows
int num_cols() const; //returns the number of columns
//.. some more stuff ..
private:
// ... some stuff ...
};