no matching function...

When I try to compile this program, among other error messages, I get one that says "no matching function for call to 'Matrix::Matrix()'" and I don't know why.
Can anyone help?

here is the code so far...

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>


using namespace std;

class DimensionalityError{}; //exception
class Matrix
{
private:
int rows;
int columns;
double *data;
public:
Matrix(int rows, int columns);
Matrix(const Matrix& );
~Matrix();
void add(const Matrix&);
void mult(const Matrix&);
void mult(double);
double norm();
// friend istream& operator>>(istream&, Matrix&);
// friend ostream& operator<<(ostream&, const Matrix&);
Matrix& operator=(const Matrix &m);
};
Matrix operator+(const Matrix& m1, const Matrix& m2);
Matrix operator*(const Matrix& m1, const Matrix& m2);
Matrix operator*(double c, const Matrix& m);
Matrix operator*(const Matrix& m,double c);
void print_menu();

Matrix::Matrix(int rows, int columns)
{
data=new double[columns*rows];
}

//Matrix(const Matrix& );

Matrix::~Matrix()
{
delete[]data;
}

//Matrix& Matrix::operator=(const Matrix &m);

void Matrix::add(const Matrix&m)
{
int i;
for ( i = 0; i < rows*columns; i++)
{
data[i] = data[i] + m.data[i];
}
}

void Matrix::mult(double c)
{
int i;
for (i = 0; i < rows*columns; i++)
{
data[i] = data[i]*c;
}
}

void Matrix::mult(const Matrix&m)
{
Matrix A,B;
double answer[rows*columns];
int i,j,k;
for (i=0; i<=rows; i++)
{
for (j=0; j<=columns; j++)
{
double sum[i][j];
sum[i][j] = 0;
for (k=0; k<=rows; k++)
{
sum = sum + A[i][k] * B[k][j];
//answer =sum[i][j];
}
}
}

}

double Matrix::norm()
{
Matrix A;
double fnorm = 0;
int sum = 0;
int i = 0;
for ( i = 0; i < rows*columns; i++)
{
sum = pow(data[i],2) + sum;
}

fnorm = sqrt(sum);

return fnorm;
}

istream& operator>>(istream&, Matrix&);

ostream& operator<<(ostream&, const Matrix&);





int main()
{
char choice;
Matrix A,B;
double c;
//cout.setf(ios::fixed | ios::showpoint);
do{
cout << " CGS2425 MATRIX CALCULATOR CLASS PROGRAM MENU";
cout << "0 - Quit";
cout << "1 - Enter A";
cout << "2 - Enter B";
cout << "3 - print A+B";
cout << "4 - print A*B";
cout << "5 - print c*A";
cout << "6 - print ||A||";

cin >> choice;
if(choice<'0' || choice >'6')
{
cout << "Wrong choice: enter a number between 0 and 6" << endl;
continue;
}
try{
switch(choice)
{
case '1':
cout<<"Enter rows, columns, and elements of A"<<endl;
cin>>A;
break;
case '2':
cout<<"Enter rows, columns, and elements of B"<<endl;
cin>>B;
break;
case '3':
cout << "A+B=" << A+B;
break;
case '4':
cout << "A*B=" << A*B << endl;
break;
case '5':
cout << "Enter c : "; cin >> c;
cout << "c*A=" << c*A << endl;
break;
case '6':
cout <<"||A||="<<setw(10)<<setprecision(3)<<A.norm()<<endl;
break;
}
}
catch(DimensionalityError& e)
{
cout << "Error: "
<< "Dimension does not match!"<<endl;
}
}while(choice!='0');
return 0;
};
I see that you have many definitions of kind Matrix A; but then there is no constructor which takes no arguments. Add the constructor which takes no arguments.
It's the only error you get?

I think you get
"no matching function for call to 'Matrix::Matrix()'"

because it's looking for a default contructor 'Matrix()' if you call something like
 
Matrix A,B;

and you only have a constructur with two values Matrix(int rows, int columns)

Add something like
1
2
3
4
Matrix::Matrix()
{
  data=new double[4*4];
}


and/or use only the special contructor
1
2
3
4
5
6
int main()
{
char choice;
Matrix A(3,3),B(4,4);  // example
double c;
...

Or you can add the default values for row and column
Matrix(int row = 3, int column=3);
This may work. so now if you dont pass any arguments you will have the matrix of size (3x3).
Last edited on
thank you :-)
I added the default values for row and column and now I don't get that error message anymore.

however, now I get the error message
"no match for 'operator[]' in 'A[i]'"
and
"no match for 'operator[]' in B[k]'"

Do I have to define those operators? I'm not really sure what the error message means...
The A[i] would be a good use if A was an unidimensional array. Considering you've defined Matrix as a class, that's not the proper way to call out for the members. A.data[i] would be right, but the data member is private and you don't have access to it. So, you need to define a way to access the data stored in the matrix.
Topic archived. No new replies allowed.