Trouble with default constructor

Hello, I am using c9.io to run my program and I am having trouble with the default constructor. I am trying to do the following: in he default constructor, initialize centerX and centerY to 100, 200. However I am pretty sure that I am doing it incorrectly. The line at which I believe the mistake is pointed out in the following code. Please point me out in the right direction. Thank you.

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
  #include <iostream>
using namespace std;
class Circle {                                                                  //class defintion for the Circle class (usually in it's own .h file)
private:
    double radius;                                                              // member variables
    int centerX;
    int centerY;
public:
    void setRadius(double r);                                                   // member functions
    double getRadius();
    double getArea();
    double getCircumference();
    int getCenterX();
    int getCenterY();                                                           
    Circle();                                                                   // default no-argument constructor
    Circle(double r);                                                           // one argument constructor
    ~Circle();                                                                  // destructor
};
void Circle::setRadius(double radius) {                                         //implementation of the circle class (usually in it's own .cpp file)
    this->radius = radius;                                                      // notice the use of "this"!
}

double Circle::getRadius() {
    return radius;
}

double Circle::getArea() {
    return radius*radius*3.141592653589793238462;
}

double Circle::getCircumference() {
    return 2*radius*3.141592653589793238462;
}
int Circle::getCenterX() {
    return centerX;
}
int Circle::getCenterY() {
    return centerY;
}

// default constructor
Circle::Circle() {
    radius = 0.0;
}

Circle::Circle(double radius) {
    this->radius = radius;
}
//THE ERROR IS GENERATED FROM THE FOLLOWING TWO LINES
 Circle::getCenterX (int centerX){
    this->centerX=100; 
} 




Circle::~Circle() {
    // no need to do anything
}

/*----------------------------------------------------------------------------
 * test the Circle class
 */
int main()
{
    cout << "Testing the Circle class..." << endl;

    Circle c1;
    c1.setRadius(9.0);
    cout << "Radius: " << c1.getRadius() << endl;
    cout << "Area: " << c1.getArea() << endl;

    Circle* pC1 = new Circle;
    pC1->setRadius(9.0);
    cout << "Radius: " << pC1->getRadius() << endl;
    cout << "Area: " << pC1->getArea() << endl;

    Circle c2(11.0);
    cout << "Radius: " << c2.getRadius() << endl;
    cout << "Area: " << c2.getArea() << endl;

    Circle* pC2 = new Circle(11.0);
    cout << "Radius: " << pC2->getRadius() << endl;
    cout << "Area: " << pC2->getArea() << endl;

    return 0;
}


I get the following error:
CircleClassEx.cpp:50:33: error: ISO C++ forbids declaration of ‘getCenterX’ with no type [-fpermissive]
Circle::getCenterX (int centerX){
^
CircleClassEx.cpp:50:2: error: prototype for ‘int Circle::getCenterX(int)’ does not match any in class ‘Circle’
Circle::getCenterX (int centerX){
^
CircleClassEx.cpp:35:5: error: candidate is: int Circle::getCenterX()
int Circle::getCenterX() {
^
The only functions that don't have necessary return types are constructors.
All of your function definitions need to match the prototypes in your class definition.
Go through them one by one to make sure each matches.

1
2
3
 Circle::getCenterX (int centerX){
    this->centerX=100; 
} 

This is missing a return type, if you want it to be void,
the definition for this would be
1
2
3
4
class Circle {
  //...
 void getCenterX(int);
};

And you have to add void to the function signature as well.
1
2
3
void Circle::getCenterX (int centerX){
    this->centerX=100; 
}


Note that since you're doing the whole "getter/setter" thing, the aforementioned function doesn't make much sense. Why does a function called "getCenterX" assign 100 to centerX? That would be more of a "set" function.
Last edited on
You define getCenterX twice. Once on line 34 and again on line 50
Thank you Ganado. This helped :)
Topic archived. No new replies allowed.