Calculate area of circle and square using classes and header files

I need some help on calling functions. I have worked on this for three weeks. Time is up. Here is the errors I am receiving. Can someone tell me why is it throwing these errors?

||=== Build: Debug in Area (compiler: GNU GCC Compiler) ===|
|In function 'int main()':|

main.cpp|35|error: request for member 'getArea' in 'circle', which is of non-class type 'Circle()'|

|42|error: request for member 'getArea' in 'square', which is of non-class type 'Square()'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  main
#include <iostream>
#include "Circle.h"
#include "Square.h"


using namespace std;


int main( )
 {
    int radius = 0;
    int length = 0;
    Square square();
    Circle circle();
    int menuOption;


do
    { // Starting of while loop
       cout << endl << "_______________Calculate Area______________" << endl;
       cout << "Select an Object" << endl;
       cout << " 1: Circle" << endl;
       cout << " 2: Square" << endl;
       cout << " 0: Exit" << endl;
       cout << " Enter Your Choice: ";
       cin  >> menuOption;

      switch(menuOption)
{

case 1:
    {
     cout<< "Enter the radius : " ;
     cin>> radius;
     cout<< "Area of the circle is "<<circle.getArea();
     break;
    }
case 2:
    {
     cout<< "Enter length of one side of the square : "<<endl;
     cin >> length;
     cout<<"Area of the square is "<<square.getArea();
     }
}

}
   while(menuOption!=0);

   cout<<" ________________ THANK YOU ___________________" <<endl;
 }


circle.cpp

#include "Circle.h"
#include <iostream>


using namespace std;

Circle::Circle (int r)
    {

		radius = r;

	}

	int Circle::getRadius()const {
	return radius;
	}
    void Circle::setRadius (int r){
    radius = r;
    }
    int Circle::getArea()const{
    return radius * radius * 3.14;
    }

circle.h

#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>

using namespace std;


	class Circle {



		public:

		    Circle (int radius =0);

		    int getRadius()const;
		    void setRadius(int radius);
			int getArea ()const;

        private:

            int radius;


	};



#endif // CIRCLE_H_INCLUDED

square.cpp

#include "Square.h"
#include <iostream>

using namespace std;

Square::Square(int len)
{

		length = len;

	}
int Square::getLength()const {
	return length;
	}
	 int Square::getArea()const{
    return length * length;
    }


square.h

#ifndef SQUARE_H
#define SQUARE_H
#include <iostream>



using namespace std;

              // variables can be initialized during declaration
	class Square {


		public:

			Square (int length =0);

			int getLength()const;
		    void setLenght(int length);
			int getArea ()const;

        private:

            int length;

	};
	#endif // SQUARE_H


I have worked on this for three weeks.

If you ever get stuck for more than an hour or so, seek help. It isn't worth banging your head against the problem.

Lines 14 & 15 declare two functions named square and circle. You want to declare variables. Do it like this:
1
2
    Square square;
    Circle circle;

Line 151: setLength() is misspelled. Also, the function needs to be defined in square.cpp

Lines 75 & 98: Because the area of a circle with an integer radius can be a real number, getArea() should return a double, not an int. You don't need to make the same change to class Square because if the length is an integer, then the area is an integer also.

Line 76. PI is not 3.14. Use more digits, like 3.141592653589793

With these changes, the program works for me.
Try removing the () from lines 14 and 15, the compiler probably thinks that those lines are function prototypes not creating instances of your classes.

I crossed my fingers and followed your directions. I almost cried when you said it was working for you. Now I am getting another error.

||error: ld returned 1 exit status|

I was receiving this error earlier.
||error: ld returned 1 exit status|

That's only part of the error message. It means that something was not found during the linking stage. The rest of the message will state what that "something" was.

At a guess, you didn't include all of the .cpp files in the build.
Thank you!! I have it compiling. But it is not calculating the area. It is returning 0.
Any clue?
After you do
 
     cin>> radius;

or
 
     cin >> length;

you don't do anything further with that value. You need to use it to either modify the existing object, or construct a new one, using the supplied value.

1
2
     cin >> radius;
     circle.setRadius(radius);


1
2
     cin >> radius;
     Circle circle(radius);

The first of these makes sense, since you have an existing circle object. However, you should probably at least test the other approach to verify that the constructor is working correctly.

By the way, since the value of pi is a floating-point value, and consequently the area will be too, you might make all of the variables length, radius and function parameters and return types double rather than int.

Thank you. I have the circle working but the square is throwing this error:


main.cpp|43|undefined reference to `Square::setLength(int)'|

square.setLength(length);
Well, the message is correct. You haven't provided a definition for function
Square::setLength()

See square.cpp
Ugh.. your right. Its like proof reading your own paper. I cant thank you enough.. All is working.
Oops! I forgot to mention the change that Chervil found. Sorry about that.
Topic archived. No new replies allowed.