8

I am very new to c++ so forgive me if I have overlooked something simple. I have a class Circle:

class Circle: public Shape{
protected:
     //string name;
     Point focus;
     float radius;
private:

public:
    virtual void calculateArea();
    virtual void calculatePerimeter();
    Circle();
    Circle(Point p, float r);

};

I have two constructors, one of which is the default which I have overloaded:

Circle::Circle()
{
    Point p(1,1);
    focus = p;
    radius = 10;
    name = "Circle";
    calculatePerimeter();
    calculateArea();
    cout<<"default circle"<<endl;
}
Circle::Circle(Point p, float r)
{
    focus = p;
    radius = r;
    name = "Circle";
    calculatePerimeter();
    calculateArea();
}

In my main I try to create two circles one using the each constructor, however the Circle being created with Circle() never gets created. I cannot for the life of me figure out why? There are no error messages or anything.

int main{
    Circle circle(a, 3.3);
    Circle c2();
}
MichelleJS
  • 759
  • 3
  • 16
  • 32

2 Answers2

31
 Circle c2();

Does not create an object, it declares a function by name c2 which takes no argument and returns a Circle object. If you want to create a object just use:

Circle c2;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    That worked perfectly, thanks. I may have eventually noticed that if it wasn't 3am. I think its quitting time now. – MichelleJS Oct 27 '13 at 07:32
  • 2
    Or better yet: `Circle c2{}` which is value-initialization and `Circle c2;` is default-initialization. – Nawaz Oct 27 '13 at 07:50
  • 6
    Oh BTW, that is not *most* vexing parse. It is just *vexing* parse. `Type1 a(Type2());` is *most* vexing parse. – Nawaz Oct 27 '13 at 07:52
  • @Nawaz, can you elaborate on the difference of Circle c2{ }; and Circle c2; ? that'll help others. – Chan Kim Jun 14 '16 at 00:33
  • 1
    @ChanKim: Search *value-initialization* and *default-initialization* on this site. – Nawaz Jun 14 '16 at 02:24
8

This here is not an instantiation, but a function declaration:

// parameter-less function c2, returns a Circle.
Circle c2();

You need

Circle c2;

or

Circle c2{};  // requires c++11
juanchopanza
  • 223,364
  • 34
  • 402
  • 480