Issue With Overloading Constructors

I was doing this exercise from the book I'm learning from. It wanted me to make a class with a default constructor and a constructor with a parameter and it wanted itsRadius to be a pointer on the free store.

When I use the constructor with the parameter, there are no problems. When I use the default constructor, it'll make the object but I can't access any of its data.

The following code returns the error "request for member `p' in `Circle2', which is of non-class type `SimpleCircle ()()'".

The issue is with the line "Circle2.p = 22;". If I change Circle2 to Circle1 or create Circle2 with a parameter, it works. I don't quite understand what went wrong.

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
#include <cstdlib>
#include <iostream>

using namespace std;

class SimpleCircle
{
public:
       SimpleCircle();
       SimpleCircle( int r );
       ~SimpleCircle();
       int get_radius() const { return *itsRadius; }
       void set_radius( int x ) { *itsRadius = x; }
       int p;
       
private:
        int *itsRadius;
};

SimpleCircle::SimpleCircle()
{
   itsRadius = new int;
  *itsRadius = 5;                  
}

SimpleCircle::SimpleCircle( int r )
{
   itsRadius = new int;
  *itsRadius = r;                            
}

SimpleCircle::~SimpleCircle()
{

  delete itsRadius;
  itsRadius = NULL;                             
                             
}

int main(int argc, char *argv[])
{
    SimpleCircle Circle1(8);
    SimpleCircle Circle2();
    Circle2.p = 22;
    cin.get();
    return 0;
}


Thanks for your time.
Last edited on
shrine spirit wrote:
and it wanted itsRadius to be a pointer on the free store.
Can we have the name of the book and it's author?

As for your problem, on line 43 you are actually declaring a function named Circle2 that returns a SimpleCircle by value - this is an issue called Most Vexing Parse (you can google that for more info).

To fix it, just remove the parens on line 43 - in C++ you never use parens for the default constructor (this isn't Java!). In C++11 you can use curly braces if you really really want to, but there's no reason to.
Last edited on
Thanks for the help.

The book is Sam(')s Teach Yourself C++ in 21 Days by Jesse Liberty. It's a decent book... better than the other one I have by Deitel and Deitel at least.
I distinctly remember that book getting bad reviews ;) I had an idea when you used the word "free store" which is something no respectful C++ book uses.

See http://www.cplusplus.com/faq/beginners/books/
Last edited on
Topic archived. No new replies allowed.