嵌套继承LNK2001和LNK1120错误

时间:2018-11-08 19:50:43

标签: c++ class inheritance lnk2001

我的代码不起作用。我正在尝试将简单继承Shape作为基类,将Shape“ Two Dimension”和“ Three Dimension”下的两个类以及该类下的shapes作为基类。这是我的代码,但是当我尝试将新类定义为Triangle时,出现错误LNK2001 LNK1120。它看起来很复杂,但是我必须获得每个对象的面积,体积和周长。 我的完整错误:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2001 unresolved external symbol "public: virtual double __thiscall TwoDimensionShape::Area(void)" (?Area@TwoDimensionShape@@UAENXZ)  Shape   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;
}

主.cpp

#include <iostream>
#include "Shape.h"
using namespace std;

int main()
{
    Triangle t1(8, 5, 0);

    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:2)

错误消息说明了一切

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

您还没有写TwoDimenionalShape::Area

您曾答应在头文件中写一个,但您没有写