gpt4 book ai didi

用于继承的 C++ 构造函数

转载 作者:行者123 更新时间:2023-11-28 03:05:52 25 4
gpt4 key购买 nike

我只是想检查一下,以便从父类继承一个构造函数,这是使用对象组合来实现它的正确方法吗?

我收到了这个错误,它们都指的是我的构造函数。

    C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text+0x16): undefine
d reference to `vtable for Square'
C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text+0x79): undefine
d reference to `vtable for Square'
C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text$_ZN6SquareD1Ev[
Square::~Square()]+0xb): undefined reference to `vtable for Square'
collect2: ld returned 1 exit status

抱歉,这是我的新脚本,因为我已尝试重写它们

ShapeTwoD.h

class ShapeTwoD
{
protected:
string name, warpSpace;
bool containsWarpSpace;
int xCord, yCord, length, breath;
public:
//constructor
ShapeTwoD();
ShapeTwoD(string, bool);
};

ShapeTwoD.cpp

ShapeTwoD::ShapeTwoD()
{
string name = "";
bool containsWarpSpace = true;
}

ShapeTwoD::ShapeTwoD(string ShapeName, bool warpspace)
{
name = ShapeName;
containsWarpSpace = true;
}

方形.h

class Square:public ShapeTwoD
{
private:
int xVal,yVal;
int newlength, newbreath;
double area;

public:
Square();
Square(string, bool, int, int);
};

正方形.cpp

Square::Square()
{
xVal = 0;
yVal = 0;
}

Square::Square(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval)
{
xVal = xval;
YVal = yval;
cout << "test test" << endl;
}

int main()
{
Square square;
cout << "hello world" << endl;
}

最佳答案

是的,还将 xValyVal 也放入初始化列表中:

Square::Square(string ShapeName, bool square, int xval, int yval):
ShapeTwoD(ShapeName, square), xVal(xval), yVal(yval)
{
}

同时为 Square() 构造基类:

Square::Square() : ShapeTwoD(..., ...)
{
}

关于用于继承的 C++ 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19739411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com