【问题标题】:Runtime error in a class using virtual void functions but no compilation error使用虚拟 void 函数但没有编译错误的类中的运行时错误
【发布时间】:2012-09-02 20:55:11
【问题描述】:

这是我的代码:

/*
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

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

【问题讨论】:

    标签: c++ inheritance virtual function-pointers runtime-error


    【解决方案1】:

    您的 shapes 变量未初始化(我认为它是数组),您需要使用对象的地址对其进行初始化:

    shape* shapes[2] ;
    //...
    
    shapes[0] = &R;
    shapes[1] = &C;
    
    shapes[0]->displayArea();
    shapes[1]->displayArea();
    

    【讨论】:

      【解决方案2】:

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

      其次你不能这样做

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

      as 形状是您尚未为其分配内存的指针。如果你想要一个形状指针数组,你必须将形状声明为一个数组,如果它是一个 pointers 数组,你应该将 pointers 分配给它的元素而不是对象。

      EDIT 我看到事实上你确实有 setter 函数,并且你已经将它们称为 get_length() get_radius() 等。这非常令人困惑,你最好使用变量名来表示他们在可能的情况下做什么。对于阅读您的代码的人来说,这非常令人困惑

      【讨论】:

        【解决方案3】:

        线

        shape* shapes;
        

        声明一个指向形状对象的指针。但是,您没有为任何形状分配任何内存。然后你稍后使用这个指针作为一个数组:

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

        这会导致未定义的行为,因为此内存位置未分配给您的程序使用。

        【讨论】:

          【解决方案4】:

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

          shape* Shapes[2];
          

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-12-26
            • 1970-01-01
            • 2014-06-19
            • 1970-01-01
            • 2018-10-23
            • 1970-01-01
            • 2017-08-12
            • 1970-01-01
            相关资源
            最近更新 更多