C++ c++;错误:抽象类类型为“”的对象&引用;不允许

C++ c++;错误:抽象类类型为“”的对象&引用;不允许,c++,C++,我想在我的程序中使用多态性,但不知道创建时为什么使用多态性 virtual void setVertices()=0; 在类C图中,我得到一个错误 C2259: 'CRectangle': cannot instantiate abstract class (line 63 and 74) IntelliSense: object of abstract class type "CRectangle" is not allowed: pure virtual function "CFigure

我想在我的程序中使用多态性,但不知道创建时为什么使用多态性

virtual void setVertices()=0;
在类C图中,我得到一个错误

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,因为我有:

 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;
}
#包括
#包括
#包括
使用名称空间std;
类Point2D{
int x,y;
公众:
void setX(int X){X=X;}
void setY(int Y){Y=Y;}
int getX(){return x;}
int getY(){return y;}
};
类别C图:公共点2D
{
受保护的:
点2D垂直[4];
公众:
CFigure(){}
//虚空集合顶点(CFigure&fig)=0;
virtual void setVertices()=0;//如果我对这一行进行注释,所有操作都正常
};
类CRectangle:公共CFigure
{
公众:
CRectangle(){}
void集合顶点(CRectangle和fig)
{
//斜角无花果;
int-x1,y1,a;
cout-x1>>y1;
库塔;
fig.Vert[0].setX(x1);
图Vert[0].setY(y1);
图Vert[1].setX(x1+a);
图Vert[1].setY(y1);
图Vert[2].setX(x1);
图[2].setY(y1+a);
图Vert[3].setX(x1+a);
图[3].setY(y1+a);
}
无效显示点()
{
斜角f;
集合顶点(f);
对于(int i=0;i<4;i++)
{

cout
CFigure
setVertices()
声明为:

virtual void setVertices()=0;
void setVertices(CRectangle& fig)
但是
CRectangle
声明
setVertices()
为:

virtual void setVertices()=0;
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;
}
#包括
#包括
#包括
使用名称空间std;
类Point2D
{
int x,y;
公众:
void setX(int X){X=X;}
void setY(int Y){Y=Y;}
int getX(){return x;}
int getY(){return y;}
};
类别C图:公共点2D
{
受保护的:
点2D垂直[4];
公众:
CFigure(){}
虚空集合顶点()=0;
};
类CRectangle:公共CFigure
{
公众:
CRectangle(){}
void setVertices()
{
int-x1,y1,a;
cout-x1>>y1;
库塔;
垂直[0].setX(x1);
垂直[0].setY(y1);
顶点[1].setX(x1+a);
Vert[1].setY(y1);
Vert[2].setX(x1);
Vert[2].setY(y1+a);
垂直[3].setX(x1+a);
Vert[3].setY(y1+a);
}
无效显示点()
{
设置顶点();
对于(int i=0;i<4;i++)
{

CUT C++对虚拟函数没有逆反作用。是的,覆盖的版本必须与纯虚拟版本匹配,它需要什么参数。当你声明纯类函数在基类中时,你需要指定它应该拥有的参数。设计是很有缺陷的。你通常不使用<代码> STD::CIN < /代码>函数。类似于
setVertices()
但传递
std::istream&
参数的操作。
void setVertices(CRectangle&fig)
对于成员函数没有意义。请改为对
进行更改,并使函数
void setVertices()
。另外,我不明白为什么一个
CFigure
是一个点,而且还包含四个点。这似乎不正确。