首页 > 解决方案 > 不能将变量“声明为抽象类型”

问题描述

我有一个名为“SHAPE”的基类,它只包含纯虚函数,其中每个方法 = 0。该基类的派生类是 TRIANGLE。它的所有方法都是从基类 SHAPE 派生的。我仔细检查了代码,但我发现代码中没有错误。为什么编译器一直对我大喊我不能声明一个类型为 TRIANGLE 的变量?

形状.h


#ifndef SHAPE_H
#define SHAPE_H

#include "POINT.h"


class SHAPE
{
    public:

        SHAPE();
        ~SHAPE();

        virtual bool isValid() const = 0;
        virtual double getArea() const = 0;

        virtual void set_sides() = 0;
        virtual POINT operator[](const int index) const = 0;
        virtual bool isInside(const POINT& other) const = 0;
};

#endif // SHAPE_H


三角形.h


#ifndef TRIANGLE_H
#define TRIANGLE_H

#include <SHAPE.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class TRIANGLE : public SHAPE
{
    public:
        TRIANGLE();
        ~TRIANGLE();

        TRIANGLE(const POINT* arr);
        TRIANGLE(const POINT& first, const POINT& second, const POINT& third);

        bool isValid() const;
        double getArea() const;
        void set_sides();
        bool isInside() const;

        POINT operator[](const int index) const;

    private:
        POINT *points = new POINT[3];
        double sides[3];
};

#endif // TRIANGLE_H


主文件


#include <iostream>
#include <stdio.h>
#include "Render.h"
#include "GAME_SYSTEM.h"
#include "POINT.H"
#include "TRIANGLE.h"
#include "QUAD.h"

using namespace std;

Render* Render::RENDER = new Render();// ignore this

int main(int argc, char* argv[])
{
    Render* RENDER = Render::getInstance();//ignore this

    POINT a(3,3);
    POINT b(3,4);
    POINT c(4,3);

    TRIANGLE bob(a,b,c);//The problem is here
    printf("%i", bob[0].get('x'));//print the x value of the first point of triangle bob.

    return 0;
}


标签: c++variablesinheritancepure-virtual

解决方案


推荐阅读