Reputation: 35
I have a class called polygon which is my base class in which I have area and perimeter and I need to derive a rectangle class from it. Right now the program below doesn't work work and it gives me the following error:
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)
It is due to destructors that I added to the program but when I remove them both it works. I did some research and found out that it might be due to me not compiling the program .cpp file correctly. Is that my problem? If not, what is my problem?
#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");
}
Upvotes: 0
Views: 308
Reputation: 320401
You declared destructors in your classes. But you never defined them. Why would you declare functions and then fail to define them? You declared polygon::~polygon()
and rectangle::~rectangle()
. Neither is defined though.
You are basically lying to the compiler. You make a promise by declaring a function, and then you break that promise by failing to define it. Hence the error.
P.S. And that's int main()
, not void main()
.
Upvotes: 2
Reputation: 182753
You didn't add destructors. You said you added them, but you didn't actually add them. So the linker is looking for them and not finding them.
You could just change:
~rectangle();
to
~rectangle() { ; }
Upvotes: 0