c ++错误:抽象类类型的对象" ..."不被允许

时间:2015-04-13 17:33:05

标签: c++

我想在程序中使用多态,但在创建

时不知道为什么
virtual void setVertices()=0;
在类CFigure中

我收到错误

C2259: 'CRectangle': cannot instantiate abstract class (line 63 and 74)
IntelliSense: object of abstract class type "CRectangle" is not allowed:
pure virtual function "CFigure:setVertices" has no overrider (line 63 and 74)

我还要声明:

virtual void setVertices(CFigure& fig) = 0;

我完全不知道如果我能写出CFigure& fig cuz CRectangle我有:

 void setVertices(CRectangle& fig)

这两种方法有不同的参数。 有人可以告诉我如何帮助我解释这些错误并告诉我如何修复我的程序?代码:

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

class Point2D{
    int x, y;
public:
    void setX(int X){ x = X; }
    void setY(int Y){ y = Y; }

    int getX(){ return x; }
    int getY(){ return y; }

};


class CFigure :public Point2D
{
protected:
    Point2D Vert[4];

public:
    CFigure(){}

    //virtual void setVertices(CFigure& fig) = 0;
    virtual void setVertices()=0;// if I comment this line all works good
};

class CRectangle : public CFigure
{

public:
    CRectangle(){}

    void setVertices(CRectangle& fig)
    {
        //CRectangle fig;
        int x1, y1, a;
        cout << "Give x1, y1" << endl;
        cin >> x1 >> y1;
        cout << "Give a" << endl;
        cin >> a;

        fig.Vert[0].setX(x1);
        fig.Vert[0].setY(y1);

        fig.Vert[1].setX(x1 + a);
        fig.Vert[1].setY(y1);

        fig.Vert[2].setX(x1);
        fig.Vert[2].setY(y1 + a);

        fig.Vert[3].setX(x1 + a);
        fig.Vert[3].setY(y1 + a);

    }


    void showPoints()
    {
        CRectangle f;
        setVertices(f);
        for (int i = 0; i < 4; i++)
        {
            cout << "P" << i << "( " << f.Vert[i].getX() << " " << f.Vert[i].getY() << " ) " << endl;
        }
    }

};
int main()
{
    CRectangle ag;
    ag.showPoints();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

CFiguresetVertices()声明为:

virtual void setVertices()=0;

CRectanglesetVertices()声明为:

void setVertices(CRectangle& fig)

附加参数使得CRectangle::setVertices()不会覆盖 CFigure::setVertices()。它反而是重载。这就是为什么编译器抱怨CRectangle是一个抽象类 - 它确实是。当覆盖虚拟方法时,覆盖方法的签名必须完全匹配被覆盖的方法的签名,例如:

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

class Point2D
{
    int x, y;
public:
    void setX(int X){ x = X; }
    void setY(int Y){ y = Y; }

    int getX(){ return x; }
    int getY(){ return y; }    
};

class CFigure : public Point2D
{
protected:
    Point2D Vert[4];

public:
    CFigure() {}

    virtual void setVertices()=0;
};

class CRectangle : public CFigure
{
public:
    CRectangle() {}

    void setVertices()
    {
        int x1, y1, a;
        cout << "Give x1, y1" << endl;
        cin >> x1 >> y1;
        cout << "Give a" << endl;
        cin >> a;

        Vert[0].setX(x1);
        Vert[0].setY(y1);

        Vert[1].setX(x1 + a);
        Vert[1].setY(y1);

        Vert[2].setX(x1);
        Vert[2].setY(y1 + a);

        Vert[3].setX(x1 + a);
        Vert[3].setY(y1 + a);
    }

    void showPoints()
    {
        setVertices();
        for (int i = 0; i < 4; i++)
        {
            cout << "P" << i << "( " << Vert[i].getX() << " " << Vert[i].getY() << " ) " << endl;
        }
    }    
};

int main()
{
    CRectangle ag;
    ag.showPoints();
    return 0;
}