Наследование вложенности LNK2001 и LNK1120 ошибка - PullRequest
0 голосов
/ 08 ноября 2018

Мой код не работает. Я пробую простое наследование Shape в качестве базового класса и двух классов в Shape "Two Dimension" и "Three Dimension" и фигур в этих классах. Вот мой код, но когда я пытаюсь определить новый класс как треугольник, он выдает ошибку 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();
};

my .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;
}

1 Ответ

0 голосов
/ 08 ноября 2018

в сообщении об ошибке все сказано

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

Вы не написали TwoDimenionalShape::Area

В вашем заголовочном файле вы обещали написать один, но вы не сделали

...