繁体   English   中英

使用虚拟void函数的类中的运行时错误,但没有编译错误

[英]Runtime error in a class using virtual void functions but no compilation error

这是我的代码:

/*
create an abstract class shape and derived classes rectangle and circle from class shape, implement abstract method of class shape in rectangle and circle. Class circle contains radius as data members rectangle class contains length and breadth.
 */

class shape
{

              virtual void displayArea() = 0;
              virtual void get_radius(double r) = 0;
              virtual void get_length(double a) = 0;
              virtual void get_breadth(double b) = 0;
};

class rectangle: public shape
{

      protected:

                double length;
                double breadth;
      public:
             virtual void get_length(double a)
             {
                     length = a;
             }
             virtual void get_breadth(double b)
             {
                     breadth = b;
             }
             virtual void get_radius(double r)
             {
                     cout << endl;
             }
             virtual void displayArea()
             {
                     cout << "Area of RECTANGLE = " << length*breadth << endl;
             }
};

class circle: public shape
{

      protected:
                double radius;
      public:
              virtual void get_length(double a)
             {
                     cout << endl;
             }
             virtual void get_breadth(double b)
             {
                     cout << endl;
             }
             virtual void get_radius(double r)
             {
                     radius = r;
             }
             virtual void displayArea()
             {
                     cout << "Area of circle = " << 3.14*radius*radius << endl;
             }
};

int main()
{

    shape* shapes;
    double l, r, b;
    rectangle R;
    circle C;

    cout << "Enter the length and breadth for rectangle\n" << endl;
    cin >> l >> b; 
    cout << "\nEnter the radius of circle\n " << endl;
    cin >> r;
    R.get_length(l);
    R.get_breadth(b);
    C.get_radius(r);

    shapes[0] = R;
    shapes[1] = C;

    shapes[0].displayArea();
    shapes[1].displayArea();

    system("pause");
    return 0;
}

它没有任何编译错误,但是在运行时会发生这种情况:输入矩形的长度和宽度

3 3

输入圆的半径

3

然后暂停一段时间,然后终止。 我很困惑,我在这里做错了什么以及如何纠正? 代码有什么问题?

您的shapes变量尚未初始化(我想它是数组),您需要使用对象的地址来初始化它:

shape* shapes[2] ;
//...

shapes[0] = &R;
shapes[1] = &C;

shapes[0]->displayArea();
shapes[1]->displayArea();

首先,您没有构造函数或setter函数(至少没有显示任何代码),因此无法设置类成员变量。

其次,你不能这样做

shape* shapes;
shapes[0] = R;
shapes[1] = C;

因为形状是尚未分配内存的指针。 如果要使用形状指针数组,则必须将形状声明为数组,如果它是指针数组,则应将指针分配给其元素而不是对象。

编辑我看到实际上您确实有setter函数,并且您已经将它们称为get_length() get_radius()等。这非常容易混淆,您最好使用变量名来指出它们在可能的情况下的作用。 对于阅读您的代码的人来说,这非常令人困惑

线

shape* shapes;

声明一个指向形状对象的指针。 但是,您尚未为任何形状分配任何内存。 然后,您以后可以将此指针用作数组:

shapes[0] = R; 
shapes[1] = C; 

因为未分配此内存位置供程序使用,所以这将导致未定义的行为。

我注意到您的代码的一件事是,“形状”实际上不是数组。 它是一个未初始化的指针。 因此,shapes [0] = R不会将member#0指向R,而是将其复制到指针指向的任何位置。 下一行将执行类似的操作。 这可能会导致内存损坏。 您可能想要类似

shape* Shapes[2];

甚至(将这行移动到声明位置C和R之后的位置):

shape** Shapes = {
    &R,
    &C
};

暂无
暂无

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

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