doesn't compile this program

hi,
I wrote this program.What's the problem of this program?It doesn't compile me.I think It hasn't got any problem inside.
Tanx


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
#include <iostream>
using namespace std;
 
class Circle
{
	float s1,r;
public:
	void input_circle()
		float output_circle()
		friend void equal(Rectangle M,Circle N)

};

void Circle::input_circle()
{
	cin>>r;
}
float Circle::output_circle()
{
	return s1=(r*r*3.1);
}
class Rectangle
{
	float s2,x;
public:
	void input()
		float output()
		friend void equal(Rectangle M, Circle N)
};
void Rectangle::input()
{
	cin>>x;
}
float Rectangle::output()
{
	return s2=(x*x;)
}
int main()
{
	Rectangle O1;
	Circle O2;
	O2.input_circle();
	O2.output_circle();
	O1.input();
	O1.output();
	equal(O1,O2);
		
}

float equal(Rectangle M ,Circle N)
{
	if  (M.S2==N.S1)
		cout<<"It's equal";
	else
		cout<<"It's not equal";
}
Last edited on
you need a semicolon after each prototype (lines 8,9,10,etc)

on line 43/45 you need somewhere a cout otherwise nothing will happen.
I fix them according to your saying .but it doesn't compile still.
1
2
3
4
5
6
7
	Rectangle O1;
	Circle O2;
	O2.input_circle();
	cout<<O2.output_circle();
	O1.input();
	cout<<O1.output();
	equal(O1,O2);
So how's your code now and what does the error say on which line

On line 36: the semicolon must be at the very end of the line
The return type of the friend declaration of equal does not match the definition of equal.

When you declare equal on line 10 it doesn't know what Rectangle is, so it will not compile. If you change the arguments of the equal function to be references you can make it work by putting a forward declaration of Rectangle before the class definition of Circle. Using references also has the advantage that the objects doesn't need to be copied every time they are passed to the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...

class Rectangle;

class Circle
{
	...
	friend void equal(const Rectangle& M, const Circle& N);
};

...

class Rectangle
{
	...
	friend void equal(const Rectangle& M, const Circle& N);
};

...

void equal(const Rectangle& M, const Circle& N)
{
	...
}

Last edited on
I changed my code.Is It true?It doesn't compile still.plz leave true code here.
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
#include <iostream>
using namespace std;
class Rectangle;
class Circle
{
	float s1,r;
public:
	void input_circle();
		float output_circle();
		friend void equal(Circle& M ,Rectangle& N);

};

void Circle::input_circle()
{
	cin>>r;
}
float Circle::output_circle()
{
	return s1=(r*r*3.1);
}
class Circle;
class Rectangle
{
	float s2,x;
public:
	void input();
		float output();
		friend void equal(Circle& M ,Rectangle& N);
};
void Rectangle::input()
{
	cin>>x;
}
float Rectangle::output()
{
	return s2=(x*x);
}
int main()
{
	Rectangle O2;
	Circle O1;
	O1.input_circle();
	cout<<O1.output_circle();
	O2.input();
	cout<<O2.output();
	equal(O1,O2);
		
}

void equal(Circle& M ,Rectangle& N)
{
	if  (M.S1==N.S2)
	{
		cout<<"It's equal";
	}
	else
	{
		cout<<"It's not equal";
	}
}
Last edited on
What are the compiler error messages?

Usually, the message will indicate the line of code where there's a problem, and should guide you to make the corrections to the code.
These are error messages:

Compilation finished with errors:
source.cpp: In function 'void equal(Circle, Rectangle)':
source.cpp:53:11: error: 'class Circle' has no member named 'S1'
source.cpp:53:17: error: 'class Rectangle' has no member named 'S2'
Line 53, it should be M.s1 == m.s2
It fixed .problem was from here:
if (M.S1==N.S2)
to
if (M.s1==N.s2)
Topic archived. No new replies allowed.