未调用 C++ 默认构造函数

2021-12-30 00:00:00 class constructor c++

我对 C++ 很陌生,所以如果我忽略了一些简单的东西,请原谅我.我有一个圈子类:

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();
}

在我的主要内容中,我尝试使用 each 构造函数创建两个圆圈,但是使用 Circle() 创建的圆圈从未被创建.我终其一生都无法弄清楚为什么?没有错误消息或任何内容.

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();
}

推荐答案

 Circle c2();

不创建对象,它通过名称 c2 声明一个函数,该函数不接受任何参数并返回一个 Circle 对象.如果你想创建一个对象,只需使用:

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;

相关文章