Program that uses classes and member functions...

Hi,

So I wrote a program that uses a class named "LinearEquation" to calculate X and Y the two linear equations(formulas in the code). This is the compile log with the errors. I think it may have to do with the way I am using LinearEquation with the functions, specifically in the main program. I wonder if it has to do with the declaring of objects so that I can use the LinearEquation class in the main program portion with the member functions I created in the member functions definitions portion of the code. any help would be much appreciated.

main.cpp: In function 'int main()':
main.cpp:103:19: error: expected primary-expression before '.' token
if(LinearEquation.isSolvable() == true)
^

main.cpp:105:35: error: expected primary-expression before '.' token
cout << "X is " << LinearEquation.getX() << " and Y is " << LinearEquation.getY();
^
main.cpp:105:76: error: expected primary-expression before '.' token
cout << "X is " << LinearEquation.getX() << " and Y is " << LinearEquation.getY();
^

C:\Users\dusti\OneDrive\Documents\Dev-C++\LinearEquations2\Makefile.win:28: recipe for target 'main.o' failed

mingw32-make.exe: *** [main.o] Error 1


Compilation results...
--------
- Errors: 3
- Warnings: 0
- Compilation Time: 0.56s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>            //header containing cout and cin

using namespace std; //introduces namespace std needed to use cout and cin

class LinearEquation
{
	private:    //data members not accessible to main
		double a,b,c,d,e,f;			
		
	public:	   //member functions accessible to main
		LinearEquation(double,double,double,double,double,double);     
		double geta();    
		double getb();    
		double getc();    
		double getd();    
		double gete();    
		double getf();    
		double getX();   
		double getY();       
		bool isSolvable();    
		
};

//member function definitions
LinearEquation::LinearEquation(double g,double h,double i,double j,double k,double l)        //six argument constructor, contains default fan parameters
{
	a=g;
	b=h;
	c=i;
	d=j;
	e=k;
	f=l;
}
double LinearEquation::geta()
{
	return a;
}
double LinearEquation::getb()
{
	return b;	
}
double LinearEquation::getc()
{
	return c;	
}
double LinearEquation::getd()
{
	return d;	
}
double LinearEquation::gete()
{
	return e;	
}
double LinearEquation::getf()
{
	return f;	
}
bool LinearEquation::isSolvable()
{
	if(a*d-b*c != 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
double LinearEquation::getX()
{
	double X;
	X = (e*d-b*f)/(a*d-b*c);
	return X;
}
double LinearEquation::getY()
{
	double Y;
	Y = (a*f-e*c)/(a*d-b*c);
	return Y;
}

int main()
{
	double g,h,i,j,k,l;
	cout << "Enter a, b, c, d, e, f: ";
	cin >> g >> h >> i >> j >> k >> l;
	LinearEquation(g,h,i,j,k,l);
	if(LinearEquation.isSolvable() == true)
	{	
	cout << "X is " << LinearEquation.getX() << " and Y is " << LinearEquation.getY();	
	}
	else
	{
	cout << "\nThe equation has no solution";
	}
	return 0;
}
Last edited on
Do you know that LinearEquation is the name of the class not the name of an instance of the class?

You need to create an instance of the class and use the name of the instance, not the name of the class.

Your problem starts at line 89. That is not how you create an instance of the class.

Thanks for the reply, that was the wording(instance) I was looking for.

Can you give an example? That is precisely the part I am struggling with trying to figure out.
Look at line 84, that line is creating several instances of type double, named g,h,i,j,k,l.

Remember a class is creating a User Defined Type.

Last edited on
I tried defining my own for the class shown in the below code but it returns an error "[Error] no matching function for call to 'LinearEquation::LinearEquation()".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	double g,h,i,j,k,l;
	LinearEquation obj1; 
	cout << "Enter a, b, c, d, e, f: ";
	cin >> g >> h >> i >> j >> k >> l;
	LinearEquation(g,h,i,j,k,l);
	if(obj1.isSolvable() == true)
	{	
	cout << "X is " << obj1.getX() << " and Y is " << obj1.getY();	
	}
	else
	{
	cout << "\nThe equation has no solution";
	}
	return 0;
}


From what I understand this is how your supposed to define it, but still not working.
Last edited on
Remove line 4.
Replace line 7 with LinearEquation obj1(g,h,i,j,k,l);
Ok I understand, that worked I appreciate the help.
Topic archived. No new replies allowed.