我有一个名为多边形的类,它是我的基类,其中我有面积和周长,我需要从中派生一个矩形类。现在下面的程序不起作用,它给了我以下错误:
GS_Inheritance_Program.obj : error LNK2019: unresolved external symbol "public: virtual
__thiscall rectangle::~rectangle(void)" (??1rectangle@@UAE@XZ) referenced in function
"public: virtual void * __thiscall rectangle::`scalar deleting destructor'(unsigned int)"
(??_Grectangle@@UAEPAXI@Z)
这是由于我添加到程序中的析构函数,但是当我删除它们时,它都可以工作。我做了一些研究,发现这可能是由于我没有正确编译程序 .cpp 文件。那是我的问题吗?如果没有,我的问题是什么?
#include <iostream>
using namespace std;
class polygon
{
protected:
double area;
double perimeter;
public:
polygon(){}
~polygon();
double printperimeter();
double printarea();
};
double polygon::printperimeter()
{
return perimeter;
}
double polygon::printarea()
{
return area;
}
class rectangle:public polygon
{
protected:
double length;
double width;
public:
rectangle(double = 1.0, double = 1.0);
~rectangle();
double calcarea();
double calcperimeter();
};
rectangle::rectangle(double l, double w)
{
length = l;
width = w;
}
double rectangle::calcarea()
{
area = length*width;
return printarea();
}
double rectangle::calcperimeter()
{
perimeter = 2*(length+width);
return printperimeter();
}
void main()
{
rectangle rect_1 (9.0, 5.0);
cout<<"The Area of Rect_1 is " <<rect_1.calcarea() <<endl;
system("pause");
}