简体   繁体   English

在另一个C ++类中创建对象

[英]Creating an object within another class C++

I'm getting a strange runtime error that I simply cannot comprehend. 我遇到了一个我完全无法理解的奇怪的运行时错误。 I'm making an object of my class Circle, which has the following default constructor: 我正在创建我的Circle类的对象,该对象具有以下默认构造函数:

    Circle::Circle()
{
    this->radius = 0;
    this->center->setX(0);
    this->center->setY(0);
}

The variables that are being are initialized are: 正在初始化的变量是:

private:
    double radius;
    Point *center;
};

When I try to make an object of the class circle, I get a runtime error. 当我尝试创建类圆的对象时,出现运行时错误。 Now I only get this error when the Point objectis declared dynamically. 现在,只有动态声明Point对象时,才会出现此错误。 Is there anything wrong with my syntax? 我的语法有什么问题吗? When I declare the Point in my Circle class like this instead: 当我像这样在Circle类中声明Point时:

Point center;

And initializes it like this instead: 并像这样初始化它:

Circle::Circle()
{
    this->radius = 0;
    this->center.setX(0);
    this->center.setY(0);
}

It works. 有用。 Why do I get these errors when I create the object dynamically? 动态创建对象时为什么会出现这些错误? Can I not use two "->" like in the first example? 我可以不像第一个示例那样使用两个“->”吗?

This is my first post, I hope this is not a too stupid question. 这是我的第一篇文章,我希望这不是一个太愚蠢的问题。 Thanks in advance. 提前致谢。

You should better use 你最好用

Circle {
private:
    double radius;
    Point center; // <<<<<<< No pointer here
};

You don't need a pointer. 您不需要指针。

The problem with your current code is that no memory is allocated for your pointer variable. 当前代码的问题是没有为指针变量分配内存。 I'd also not recommend to do it (eg with center = new Point() ). 我也不建议这样做(例如,使用center = new Point() )。 As mentioned, it's not necessary. 如前所述,没有必要。

Also you don't need this-> to access class members. 另外,您不需要this->访问班级成员。 Just use the member initializer list in your constructor: 只需在构造函数中使用成员初始化器列表即可:

Circle::Circle() : radius(0), center(0,0) {
}

center is a pointer. center是一个指针。 If you do not allocate any memory for it then you cannot access it as it does not point to a valid object. 如果您没有为其分配任何内存,那么您将无法访问它,因为它没有指向有效的对象。 To get a valid object we would use 为了获得有效的对象,我们将使用

Circle::Circle() : radius(0), center(new Point)
{
    center->setX(0);
    center->setY(0);
}

If Point has a constructor that takes x and y then you could even use 如果Point的构造函数采用xy那么您甚至可以使用

Circle::Circle() : radius(0), center(new Point(0, 0)) {}

But I do have to ask if you even need a pointer here. 但是,我确实必须问您在这里是否甚至需要一个指针。 If not then you could have 如果没有的话,你可能有

Circle::Circle() : radius(0),
{
    center.setX(0);
    center.setY(0);
}
// or
Circle::Circle() : radius(0), center(0, 0) {}

The problem occurs because center is a pointer that does not have memory allocated to it. 发生问题的原因是center是没有分配内存的指针。 Since no such object exists, 由于不存在这样的对象,

Circle::Circle()
{
    this->radius = 0;
    this->center = new Point; //call the appropriate Point constructor
    this->center->setX(0); //now these are valid
    this->center->setY(0);
}

also notice since center is actually a pointer in your implementaion, center.setX(0) is invalid, you should do center->setX(0) instead 还要注意,因为center实际上是实现中的指针, center.setX(0)无效,您应该center.setX(0) center->setX(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM