错误处理1: D:a1-C++C++作业第五次1.cpp undefined reference to `vtable for Shape'

在编译程序的时候遇到此误,在google上查,很多地方都说是因为虚基类里面的虚拟析构函数没有提供实现导致的。但是我的已经提供了实现,也不行。最后发现是其他没有提供实现的虚函数造成的。所以,在一个虚基类里,如果不提供函数的缺省实现,一定要定义成纯虚函数,否则就会造成此问题。

#include <iostream> using namespace std; #define pi 3.14

class Shape{  public:   static double sum;   virtual void show()const; };

double Shape::sum = 0;

class Circle : public Shape{  private:   double r;  public:   Circle(double r1):r(r1){    sum += r * r * pi;   }    void show()const{    cout << "圆:" << endl;    cout << "圆的半径:" << r << endl;    cout << "圆的面积:" << r * r * pi << endl;   } }; class Rectanglez: public Shape{  private:   double x;  public:   Rectanglez(double x1): x(x1){    sum += x * x;   }    void show()const{    cout << "正方形:" << endl;    cout << "正方形的边长:" << x << endl;    cout << "正方形的面积:" << x * x << endl;   } }; class Rectanglec: public Shape{  private:   double x, y;  public:   Rectanglec(double x1, double y1): x(x1), y(y1){    sum += x * y;   }   void show()const{    cout << "长方形:" << endl;    cout << "长方形的长、宽: " << x << y << endl;    cout << "长方形的面积:" << x * y << endl;   } }; int main(){  Shape *p;  char flag = 'Y';  while(toupper(flag) == 'Y'){   int select;   cout << "请选择需要输入的类型:1(圆), 2(正方形), 3(长方形)" << endl;   cin >> select;   switch(select){    case 1:     double r;     cin >> r;     p = new Circle(r);     p->show();     delete p;     break;    case 2:     double x;     cin >> x;     p = new Rectanglez(x);     p->show();     delete p;     break;    case 3:     double x1, y;     cin >> x1 >> y;     p = new Rectanglec(x1, y);     p->show();     delete p;     break;    default:     cout << "输入错误!" << endl;     break;   }   cout << "是否继续输入:N or Y ?" << endl;   cin >> flag;  }  return 0; }

原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/6896485.html