I'm New to C++ Please Help

Hey guys,

My code is not working at all. I am trying to have the user run the code, input a matrix and then have it read back the inputted matrix and then give the inverse if it can be calculated (ie. det not = to 0). Output should read:

Matrix 1:
[ 1.00000 2.00000]
[ 3.00000 4.00000]
Inverse of Matrix 1:
[ -2.00000 1.00000]
[ 1.50000 -0.50000]

Here is the code I currently have:

#include <iostream>
#include <iomanip>

using namespace std;

class Matrix
{
private:

double a; // Matrix values
double b;
double c;
double d;
double aIn;
double bIn;
double cIn;
double dIn;
public:

double determ;
bool sing;
Matrix (); // Constructor
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
};

// Code for member functions defined outside the class


double Matrix::det() // Calculates the determinant
{
double determ;
determ = (a * d) - (c * b);
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
double aIn;
double bIn;
double cIn;
double dIn;


aIn = (d * (1/determ));
dIn = (a * (1/determ));
bIn = (-1 * b)*(1/determ);
cIn = (-1 * c)*(1/determ);
}

void Matrix::print_em()
{ // Prints matrix and its inverse
cout << setiosflags(ios::fixed) << setprecision(2); // Sets outputs for one decimal
if (sing == 1)
{
cout << "Matrix: " << endl;
cout << "[ " << a << " " << b << " ]" << endl;
cout << "[ " << c << " " << d << " ]" << endl << endl << endl;

cout << "Inverse of Function: " << endl;
cout << "[ " << aIn << " " << bIn << " ]" << endl;
cout << "[ " << cIn << " " << dIn << " ]" << endl << endl << endl;
}
else
{
cout << "Matrix: " << endl;
cout << "[ " << a << " " << b << " ]" << endl;
cout << "[ " << c << " " << d << " ]" << endl << endl << endl;

cout << "Matrix has no inverse; determinant = 0!" << endl;
cout << "[ NaN NaN ]" << endl;
cout << "[ NaN NaN ]" << endl << endl << endl;
}

}

int main()
{

Matrix mat1 (double a, double b, double c, double d); //class variables
double a, b, c, d;

cout << "What is matrix you want to check? (format: a b c d): " << endl;
cin >> a >> b >> c >> d;
mat1.set( a, b, c, d);

mat1.det(); // matrix determinant is calculated
if (mat1.isSingular()== 1) //matrix is checked for singularity
{ //If matrix 1 is not singular, calculates inverse
mat1.inverse(); //Then prints matrix and its inverse
mat1.print_em();

char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;

}
else
{
mat1.print_em(); //Otherwise, if matrix is singular

char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;

} //Prints matrix plus note that determinant = 0, no inverse matrix
return 0;

char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;

}


I am having issues mainly with my main function. I am sure I am messing up with the class and probably much more. Like I said I am extremely new to all of this. In my main function the error I am getting is that mat1 must have a class type. I have no idea what this means so if you could help me with the code AND help explain this in laymen's terms that would be great. Thanks again for your help. Errors begin in the 5th line of the main function but I am guessing they persist throughout the code.

Please help me if you can... I have been screwing, unsuccessfully, with this code for 2 days now. I am also working on a windows machine using visual studio 2010 if you need to know.
Start with a simpler version of the project and get it to work piece by piece. Build up only on good code that compiles and works... That being said, were is this "set" method you're invoking mat1.set(a, b, c, d); defined?
I will try doing that. The set method is just something I saw my prof. do in order to apply the inputted variables. I honestly have no idea and have since actually removed that line. I am not exactly sure how to start it simpler though. I am pretty sure the issue is that I don't fully understand creating a class. Also I was able to run the code when I had the main function using a mat1(1, 2, 3, 4) but when I tried to change it so that the user could input a matrix of their choice I started getting messed up.
Now I'm getting that mat1 must have a class type... I have myself turned in a circle right now, shoot.
Last edited on
gh24 wrote:
I don't fully understand creating a class.

Check the tutorial on classes on this site; then, try modeling a simpler class first. Get used to the syntax and logic. Understand why using classes can make code better before going out and confusing yourself. That's my suggestion.

(Edit) PS: to be honest, your code looks a bit sloppy. I would rethink the problem if that is an option. Come up with a better class design. There are cleaner/easier ways to implement a matrix class.
Last edited on
Using this code:


#include <iostream>
#include <iomanip>

using namespace std;

class Matrix
{
private:

double a; // Matrix values
double b;
double c;
double d;
double aIn;
double bIn;
double cIn;
double dIn;

public:

double mat1 (double, double, double, double);
double determ;
bool sing;
Matrix (double); // Constructor
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
};

// Code for member functions defined outside the class


double Matrix::det() // Calculates the determinant
{
double determ;
determ = (a * d) - (c * b);
return determ;
}

bool Matrix::isSingular() //Determines if matrix is singular
{
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
double aIn;
double bIn;
double cIn;
double dIn;


aIn = (d * (1/determ));
dIn = (a * (1/determ));
bIn = (-1 * b)*(1/determ);
cIn = (-1 * c)*(1/determ);
}

void Matrix::print_em()
{ // Prints matrix and its inverse
cout << setiosflags(ios::fixed) << setprecision(2); // Sets outputs for one decimal
if (sing == 1)
{
cout << "Matrix: " << endl;
cout << "[ " << a << " " << b << " ]" << endl;
cout << "[ " << c << " " << d << " ]" << endl << endl << endl;

cout << "Inverse of Function: " << endl;
cout << "[ " << aIn << " " << bIn << " ]" << endl;
cout << "[ " << cIn << " " << dIn << " ]" << endl << endl << endl;
}
else
{
cout << "Matrix: " << endl;
cout << "[ " << a << " " << b << " ]" << endl;
cout << "[ " << c << " " << d << " ]" << endl << endl << endl;

cout << "Matrix has no inverse; determinant = 0!" << endl;
cout << "[ NaN NaN ]" << endl;
cout << "[ NaN NaN ]" << endl << endl << endl;
}

}

int main()
{

Matrix mat1(); //class variables
double a, b, c, d;

cout << "What is matrix you want to check? (format: a b c d): " ;
cin >> a >> b >> c >> d;

mat1.det(); // matrix's determinant is calculated
if (mat1.isSingular()== 1) //matrix is checked for singularity
{ //If matrix is not singular, calculates inverse
mat1.inverse(); //Then prints matrix and its inverse
mat1.print_em();

char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;

}
else
{
mat1.print_em(); //Otherwise, if first matrix is singular

char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;

} //Prints matrix plus note that determinant = 0, no inverse matrix
return 0;

char holdExecutionWindowOpen;
std::cin >> holdExecutionWindowOpen;

}

I am now only getting an error in my main function saying:

left of '.det' must have class/struct/union
left of '.isSingular' must have class/struct/union
left of '.inverse' must have class/struct/union
left of '.printem' must have class/struct/union
left of '.printem' must have class/struct/union

But I cant define those with a class type can I?

So I also tried to add Matrix::mat1.det() but that tells me that my expression still must have a class type...
Last edited on
Matrix mat1; //no () when calling to the default constructor
Last edited on
Cool, I got rid of the () but now I am being told that no default constructor exists for class 'Matrix'.

Do I just need to add a default line in the class? If so what would that look like?

Thank you so much for your help btw, I really really appreciate it.
Also do I need this line:

double mat1 (double, double, double, double);

??

I added that thinking it was going to be a default look of sorts but that obviously isn't the case.
double mat1 (double, double, double, double); This line defines the only constructor for Matrix. If you want to make a Matrix you need to use this constructor (or define another one.)
Call that constructor like this
 
Matrix mat1( a, b, c, d );
(And sorry, I assumed you wanted to call a default constructor with your above post. I didn't actually look to see if you defined one.)

PS: Can you please format your code. Look for the <> symbol in the box of formatting options to the right of your edit box.
__________________________________________
Defining a default constructor look simply like:
1
2
3
4
5
6
7
8
class A {
    A(); //default constructor
    A(int, int) //another constructor
}
a, //calls the default constructor
b(1, 2), //calls the anther constructor with parameters (int, int)
c('a'); //ERROR, a constructor with parameters (char) hasn't been defined for class A
...
Topic archived. No new replies allowed.