嵌套继承LNK2001和LNK1120错误



我的代码无法工作。我正在尝试简单的继承Shape作为基类和Shape"二维"one_answers"三维"下的两个类以及这些类下的形状。这是我的代码,但当我试图将一个新类定义为Triangle时,它会给我错误LNK2001 LNK1120。它看起来很复杂,但我必须得到每个物体的面积、体积和周长。我的全部错误:

严重性代码描述项目文件行禁止显示状态错误LNK2001未解析的外部符号"public:virtual double __thiscall TwoDimensionShape::Area(void("(?Area@TwoDimensionShape@@UAENXZ(形状c:\Users\aleyn\documents\visual studio 2015\Projects\Shape\Shape\Source.obj 1

这是.h

#pragma once
#define M_PI 3.14159265358979323846
class Shape
{
private:
double width, height, depth;
public:
Shape(double w, double h, double d);
virtual void Display() = 0;
virtual double Area() = 0;
virtual double Perimeter() = 0;
virtual double Volume() = 0;
};
class TwoDimensionShape: public Shape
{
public:
TwoDimensionShape(double w, double h, double d = 0) :
Shape(w, h, d)
{
}
double Area();
double Perimeter();
double Volume();
void Display();
};
class ThreeDimensionShape: public Shape
{
private:
double width, height, depth;
public:
ThreeDimensionShape(double w, double h, double d) :
Shape(w, h, d)
{
depth = d;
}
double Area();
double Volume();
double Perimeter();
void Display();
};
class Triangle: public TwoDimensionShape
{
private:
double side1, side2, base;
public:
Triangle(double w, double h, double d = 0) :
TwoDimensionShape(w, h, d)
{
}
double Area()
{
return Area() / 2;
}
void setTriangleSides(double s1, double s2, double b);
double Perimeter()
{
return side1, side2, base;
}
double Volume()
{
return 0;
}
};
class Square: public TwoDimensionShape
{
public:
Square(double w, double h, double d = 0) :
TwoDimensionShape(w, h, d)
{
}
double Volume()
{
return 0;
}
};
class Rectangle: public TwoDimensionShape
{
public:
Rectangle(double w, double h, double d = 0) :
TwoDimensionShape(w, h, d)
{
}
double Volume()
{
return 0;
}
};
class Circle: public TwoDimensionShape
{
private:
double radius;
public:
Circle(double r, double a = 0, double d = 0) :
TwoDimensionShape(r, a, d)
{
radius = r;
}
double Area()
{
return M_PI * (radius) * (radius);
}
double Perimeter()
{
return 2 * M_PI * radius;
}
double Volume()
{
return 0;
}
};
class Sphere: public ThreeDimensionShape
{
private:
double radius;
public:
Sphere(double r, double a = 0, double b = 0) :
ThreeDimensionShape(r, a, b)
{
radius = r;
}
double Volume()
{
return (4 / 3 * M_PI * (radius * radius * radius));
}
double Area()
{
return 4 * M_PI * radius * radius;
}
double Perimeter()
{
return 0;
}
};
class Cylinder: public ThreeDimensionShape
{
private:
double radius, height;
public:
Cylinder(double r, double h, double a = 0) :
ThreeDimensionShape(r, h, a)
{
}
double Volume()
{
return M_PI * radius * radius * height;
}
double Area()
{
return (2 * M_PI * radius * height) + 2 * M_PI * radius * radius;
}
double Perimeter()
{
return 0;
}
};
class Cone: public ThreeDimensionShape
{
private:
double radius, height, side;
public:
Cone(double r, double h, double s) :
ThreeDimensionShape(r, h, s)
{
}
double Volume()
{
return 1 / 3 * M_PI * radius * radius * height;
}
double Area()
{
return (M_PI * radius * side) + M_PI * radius * radius;
}
double Perimeter()
{
return 0;
}
};
class RectPrism: public ThreeDimensionShape
{
public:
RectPrism(double w, double h, double d) :
ThreeDimensionShape(w, h, d)
{
}
double Area();
double Volume();
double Perimeter();
};

我的.cpp

#include "Shape.h"
#include <iostream>
using namespace std;
Shape::Shape(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
double Shape::Area()
{
return width * height;
}
double Shape::Perimeter()
{
return (width + height) * 2;
}
double Shape::Volume()
{
return width * height * depth;
}
double ThreeDimensionShape::Area()
{
return (2 * (height * width) + 2 * (depth * width) + 2 * (depth * height));
}
double ThreeDimensionShape::Volume()
{
return width * height * depth;
}
double ThreeDimensionShape::Perimeter()
{
return (4 * width + 4 * height + 4 * depth);
}
void Triangle::setTriangleSides(double s1, double s2, double b)
{
side1 = s1;
side2 = s2;
base = b;
}
void TwoDimensionShape::Display()
{
cout << "Perimeter: " << Perimeter() << endl;
cout << "Area: " << Area() << endl;
cout << "Volume: " << Volume() << endl;
}

main.cpp

#include <iostream>
#include "Shape.h"
using namespace std;
int main()
{
Triangle t1(8, 5, 0);
system("pause");
return 0;
}

错误消息显示全部为

LNK2001 unresolved external symbol "public: virtual double __thiscall TwoDimensionShape::Area(void)" 

您尚未编写TwoDimenionalShape::Area

在你的头文件中,你承诺要写一个,但你没有

最新更新