classes are killing me:(

Hi, ever since we hit classes in my intro c++ course I've been flailing. Tried playing with this code that was given in my text book. It worked just as expected in a single file. But for our assignments we need a .h file, a file for the functions, and a file for main. Since this code worked I took the next step and split it up into the three files. Naturally it's full of errors now. I think this has been where I've been going wrong. If someone could show me how to make this code work I'd be able to compare it to make assignments and I could see what I've been doing wrong. Here's the three files:
class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef CIRCLE_CLASS_H
#include <iostream>

using namespace std;

class Circle
{
	private:
		double radius;
	public:
		Circle();
		void set_radius( double r );
		double set_area();
};

Circle();
void set_radius( double r );
double get_area();
// These three function declarations are the only thing that's been 
// changed/added from the code given in the book. I think this might be where 
// I'm wrong. Unless Other things have to change due to splitting up the code 
// into the three files. ???

#endif 

functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "./circle_class.h"
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

Circle::Circle()
{
	radius = 1;
}

void Circle::set_radius( double r )
{
	if ( r >= 0 )
		radius = r;
}

double Circle::get_area()
{
	return (3.14*pow(radius, 2));
}

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "./circle_class.h"
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{

	Circle circle1, circle2;

	circle2.set_radius( 2.5 );

	cout << "Circle #1 area: " << circle1.get_area() << endl;
	cout << "Circle #2 area: " << circle2.get_area() << endl;

	return 0;

}

errors

circle_class.h:25: error: expected unqualified-id before ‘)’ token
In file included from circle_class.cpp:11:
./circle_class.h:25: error: expected unqualified-id before ‘)’ token
circle_class.cpp:29: error: no ‘double Circle::get_area()’ member function declared in class ‘Circle’
In file included from main.cpp:11:
./circle_class.h:25: error: expected unqualified-id before ‘)’ token
main.cpp: In function ‘int main()’:
main.cpp:25: error: ‘class Circle’ has no member named ‘get_area’
main.cpp:26: error: ‘class Circle’ has no member named ‘get_area’

Again thank you for any assistance, if I figure this out I might actually be able to start working on homework! :)
Last edited on
1
2
3
Circle();
void set_radius( double r );
double get_area();


These functions prototypes on lines 16-18 on your class .hpp file don't need to be there. The lines 11-13 *are* the prototypes for the functions in the class.

You also have a typo in your .hpp file, you have a member function called "set_area" that takes no arguments which I believe you meant to be "get_area."

I believe that should fix all your errors.
Your include guard is missing a #define.

1
2
3
4
5
6
#ifndef CIRCLE_CLASS_H
#define CIRCLE_CLASS_H

...

#endif 
Last edited on
I can't believe it! I could never get my last lab to compile and that's probably the reason why. None of the nearly 20 TA's caught that I was doing that. And they tell me I shouldn't be getting help here on the forums. Thank you so much!! :):):)
Topic archived. No new replies allowed.