2X2 Matrix Error Messages

Hi, I'm a newbie trying to debug my program dealing with a 2x2 matrix and 2 datasets. The program has a function to calculate the determinant, a Boolean function to determine if the matrix is singular, a function to calculate the inverse of the matrix (if possible) and a print function. Below is the code:

#include <iostream>
#include <iomanip>

using namespace std;

class Matrix
{
private:
double value1; // Matrix values
double value2;
double value3;
double value4;
public:
double determ;
bool sing;
Matrix (double, double, double, double); // Constructor with defaults
Matrix (const Matrix&); // Copy Contributor
double det (double, double, double, double); // Member function to calculate the determinant
bool isSingular (double); // Member function to determine if the matrix is singular
void inverse (double, double, double, double); // Member function to create inverse matrix
void print_em (double, double, double, double, double, double, double, double); // Member function to print the matrix and its inverse
};

// Code for member functions defined outside the class
Matrix::Matrix (double v1, double v2, double v3, double v4) // Copy constructor
{
value1 = v1;
value2 = v2;
value3 = v3;
value4 = v4;
}


Matrix::Matrix(const Matrix& oldValue)
{
value1 = oldValue.value1;
value2 = oldValue.value2;
value3 = oldValue.value3;
value4 = oldValue.value4;
}

double Matrix::det(double value1, double value2, double value3, double value4) // Calculates the determinant
{
double determ;
determ = (value1 * value4) - (value2 * value3);
return determ;
}

bool Matrix::isSingular(double determ) //Determines if matrix is singular
{ //i.e. Is the determinant equal to zero?
bool sing; //Declares boolean variable
if (determ = 0)
sing = 1;
else
sing = 0;
return sing;
}

void Matrix::inverse(double valIn1, double valIn2, double valIn3, double valIn4)
{ // Calculates the inverse of the original matrix
valIn1 = (value4 * (1/determ);
valIn4 = (value1 * (1/determ);
valIn2 = (-1 * value2)*(1/determ);
valIn3 = (-1 * value3)*(1/determ);
}

void Matrix::print_em(double value1, double value2, double value3, double value4, double valIn1, double valIn2, double valIn3, double valIn4)
{ // Prints matrix and its inverse
cout << setiosflags(ios::fixed) << setprecision(1); // Sets outputs for one decimal
if (sing = 0)
{
cout << "Matrix: " << endl;
cout << "[ " << value1 << " " << value2 << " ]" << endl;
cout << "[ " << value3 << " " << value4 << " ]" << endl << endl << endl;
cout << "Inverse of Function: " << endl;
cout << "[ " << valIn1 << " " << valIn2 << " ]" << endl;
cout << "[ " << valIn3 << " " << valIn4 << " ]" << endl << endl << endl;
}
else
{
cout << "Matrix: " << endl;
cout << "[ " << value1 << " " << value2 << " ]" << endl;
cout << "[ " << value3 << " " << value4 << " ]" << endl << endl << endl;
cout << "Matrix has no inverse; determinant = 0!" << endl;
cout << "[ N/A N/A ]" << endl;
cout << "[ N/A N/A ]" << endl << endl << endl;
}

}

int main()
{
Matrix mat1 (1, 2, 3, 4);
Matrix mat2 (1, 2, 2, 4); //Two class variables

mat1.det(); //First matrix's determinant is calculated
mat1.isSingular(); //First matrix is checked for singularity
if (sing = 0) //If matrix 1 is not singular, calculates inverse
{
mat1.inverse(); //Then prints matrix and its inverse
mat1.print_em();
}
else
{
mat1.print_em(); //Otherwise, if first matrix is singular
} //Prints matrix plus note that determinant = 0, no inverse matrix
mat2.det(); //Second matrix's determinant is calculated
mat2.isSingular(); //Second matrix is checked for singularity
if (sing = 0) //If matrix 2 is not singular, calculates inverse
{
mat2.inverse(); //Then prints matrix and its inverse
mat2.print_em();
}
else
{
mat2.print_em(); //otherwise, if second matrix is singular
} //Prints matrix plus note that determinant = 0, no inverse matrix
return 0;
}
Any advice and direction is appreciated.
 
Last edited on
Sorry, here are the error messages:
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(66) : error C2143: syntax error : missing ')' before ';'
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(67) : error C2143: syntax error : missing ')' before ';'
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(101) : error C2660: 'Matrix::det' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(102) : error C2660: 'Matrix::isSingular' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(103) : error C2065: 'sing' : undeclared identifier
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(105) : error C2660: 'Matrix::inverse' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(106) : error C2660: 'Matrix::print_em' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(110) : error C2660: 'Matrix::print_em' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(112) : error C2660: 'Matrix::det' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(113) : error C2660: 'Matrix::isSingular' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(114) : error C2065: 'sing' : undeclared identifier
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(116) : error C2660: 'Matrix::inverse' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(117) : error C2660: 'Matrix::print_em' : function does not take 0 arguments
1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(121) : error C2660: 'Matrix::print_em' : function does not take 0 arguments
1>Build log was saved at "file://c:\Users\Ron\Documents\Visual Studio 2008\Projects\Project1-Matrix\Project1-Matrix\Debug\BuildLog.htm"
1>Project1-Matrix - 14 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The errors are pretty self-explanatory.
missing ')' before ';' means that there is an unmatched '('. the line is valIn1 = (value4 * (1/determ); can you see where a ')' should be?
The other errors are because you apparently don't know how do member functions work.here's your code:
1
2
3
4
5
6
double Matrix::det(double value1, double value2, double value3, double value4)	 // Calculates the determinant 
{	
double determ;
determ = (value1 * value4) - (value2 * value3);
return determ;
}
You defined a function with four arguments. Since this is a member function, Matrix::value1-4 are already here. Parameters with the same names simply hide the members. Furthermore, you call the function as if it had no parameters (hence the 'function does not take 0 arguments'). Remove the parameter list and it should work fine.

Another one is because you use a variable that has never been declared. the right way would be either
1
2
bool sing = mat1.isSingular();
if(sing == 0) ...
orif(mat1.isSingular() == 0)...(note the ==. = is assignment, == is comparison)
hamsterman:
You are correct, I am unsure of how member functions work. My first C++ class never got to that point, and the current class assumes that knowledge, so I am trying to learn on the fly! I cleaned up the '(' errors; however, when I removed the parameter list from the functions i.e

double Matrix::det()
{
double determ;
determ = (value1 * value4) - (value2 * value3);
return determ;
}

I get these errors, followed by the 'function does not take 0 argument errors':

1>c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(48) : error C2511: 'double Matrix::det(void)' : overloaded member function not found in 'Matrix'
1> c:\users\ron\documents\visual studio 2008\projects\project1-matrix\project1-matrix\project1-matrix.cpp(12) : see declaration of 'Matrix'

If i misinterpreted what to do, sorry for coming across "dense"; it's difficult under pressure cramming this all into my head. Thanks!
You have to clean function declarations too
Thanks, I did figure that out eventually after I reposted here and the program runs. Your help has been greatly appreciated
One more question: although the program ran, it printed out all zeroes for both inverse matrices. I'm learing to debug & isSingular is always true and determ and valIn1, 2, 3 and 4 have garbage values. Is the problem in the determ calculation or declaration? Thanks in advance, here's the updated code:


#include <iostream>
#include <iomanip>

using namespace std;

class Matrix
{
private:
double value1; // Matrix values
double value2;
double value3;
double value4;
double valIn1;
double valIn2;
double valIn3;
double valIn4;
public:
double determ;
bool sing;
Matrix (double = 1, double = 2, double = 3, double = 4); // Constructor with defaults
Matrix (const Matrix&); // Copy Contributor
double det (); // Member function to calculate the determinant
bool isSingular (); // Member function to determine if the matrix is singular
void inverse (); // Member function to create inverse matrix
void print_em (); // Member function to print the matrix and its inverse
};

Last edited on
The rest of the code:


// Code for member functions defined outside the class
Matrix::Matrix (double v1, double v2, double v3, double v4) // Copy constructor
{
value1 = v1;
value2 = v2;
value3 = v3;
value4 = v4;
}


Matrix::Matrix(const Matrix& oldValue)
{
value1 = oldValue.value1;
value2 = oldValue.value2;
value3 = oldValue.value3;
value4 = oldValue.value4;
}

double Matrix::det() // Calculates the determinant
{
double determ;
determ = (value1 * value4) - (value2 * value3);
return determ;
}

bool Matrix::isSingular() //Determines if matrix is singular
{ //i.e. Is the determinant equal to zero?
bool sing; //Declares boolean variable
if (determ == 0)
sing = 0;
else
sing = 1;
return sing;
}
void Matrix::inverse()
{ // Calculates the inverse of the original matrix
valIn1 = (value4 * (1/determ));
valIn4 = (value1 * (1/determ));
valIn2 = (-1 * value2)*(1/determ);
valIn3 = (-1 * value3)*(1/determ);
}

void Matrix::print_em()
{ // Prints matrix and its inverse
cout << setiosflags(ios::fixed) << setprecision(1); // Sets outputs for one decimal
if (sing == 1)
{
cout << "Matrix: " << endl;
cout << "[ " << value1 << " " << value2 << " ]" << endl;
cout << "[ " << value3 << " " << value4 << " ]" << endl << endl << endl;
cout << "Inverse of Function: " << endl;
cout << "[ " << valIn1 << " " << valIn2 << " ]" << endl;
cout << "[ " << valIn3 << " " << valIn4 << " ]" << endl << endl << endl;
}
else
{
cout << "Matrix: " << endl;
cout << "[ " << value1 << " " << value2 << " ]" << endl;
cout << "[ " << value3 << " " << value4 << " ]" << endl << endl << endl;
cout << "Matrix has no inverse; determinant = 0!" << endl;
cout << "[ N/A N/A ]" << endl;
cout << "[ N/A N/A ]" << endl << endl << endl;
}

}

int main()
{
Matrix mat1 (1, 2, 3, 4);
Matrix mat2 (1, 2, 2, 4); //Two class variables

mat1.det(); //First matrix's determinant is calculated
if (mat1.isSingular()== 1) //First matrix is checked for singularity
{ //If matrix 1 is not singular, calculates inverse
mat1.inverse(); //Then prints matrix and its inverse
mat1.print_em();
}
else
{
mat1.print_em(); //Otherwise, if first matrix is singular
} //Prints matrix plus note that determinant = 0, no inverse matrix
mat2.det(); //Second matrix's determinant is calculated
if (mat2.isSingular()== 1) //Second matrix is checked for singularity
{ //If matrix 2 is not singular, calculates inverse
mat2.inverse(); //Then prints matrix and its inverse
mat2.print_em();
}
else
{
mat2.print_em(); //otherwise, if second matrix is singular
} //Prints matrix plus note that determinant = 0, no inverse matrix
return 0;
}
Last edited on
The problem is that in your functions you define local variables that have the same name as member variables:
1
2
3
4
5
double Matrix::det(){	
    double determ;//why define this? you already have a member variable determ. remove this line
    determ = (value1 * value4) - (value2 * value3);//here you set the value of local determ and not Matrix::determ
    return determ;
}

This problem is in many of your functions.
I took out the definition of matrix and sing and the program ran properly. I really appreciate your help. Learning programming by doing it does help, but if you are new to the concepts as I am, finding your errors is very time consuming and frustrating, because half the time you don't know what to look for. Thanks for keeping me on the correct path and being patient.
Topic archived. No new replies allowed.