继承类的复制构造函数
copy constructor of an inherited class
我试图定义一个类的复制构造函数,但我弄错了。我正在尝试使用这个构造函数做QGraphicsRectItem
的儿子:
QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 )
这里有一些代码
QtL 定义的QGraphicsRectItem
QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 )
Cell.h,儿子的班级:
Cell();
Cell(const Cell &c);
Cell(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 );
Cell.cpp:
Cell::Cell() {}
/* got error defining this constructor (copy constructor) */
Cell::Cell(const Cell &c) :
x(c.rect().x()), y(c.rect().y()),
width(c.rect().width()), height(c.rect().height()), parent(c.parent) {}
Cell::Cell(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent) :
QGraphicsRectItem(x, y, width, height, parent) {
...
// some code
...
}
错误显示:
/../../../../cell.cpp:7: error: class 'Cell' does not have any field named 'x'
/../../../../cell.cpp:7: error: class 'Cell' does not have any field named 'y'
/../../../../cell.cpp:7: error: class 'Cell' does not have any field named 'width'
/../../../../cell.cpp:7: error: class 'Cell' does not have any field named 'height'
/../../../../cell.cpp:7: error: class 'Cell' does not have any field named 'parent'
感谢
您需要按照如下方式制作复制构造函数:
Cell::Cell(const Cell &c)
:
QGraphicsRectItem(c.rect().x(), c.rect().y(),
c.rect().width(), c.rect().height(),
c.parent())
{}
原因是您的Cell
类由于继承而是QGraphicsRectItem
。因此,构造函数的c
参数也表示QGraphicsRectItem
,因此您可以使用它的QGraphicsRectItem::rect()
和QGraphicsRectItem::parent()
函数来构造新对象-c
的副本。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 使用复制构造函数复制双精度数组
- C 无可行的构造函数复制类型的变量
- 没有可行的构造函数复制类型 'MyString' 的数组元素
- 编译时,复制构造函数/复制分配和正常功能调用优化之间是否存在任何区别
- 如何最小化调用列表构造函数(复制构造函数)的次数?
- C 11矢量构造函数复制与范围
- 我定义了一个非复制构造函数;复制构造函数还会被隐式定义吗
- 可以将构造函数复制为转换运算符
- 将基类指针的构造函数复制到子类
- C++树类:构造函数/复制/内存泄漏
- 如何制作这个在模板构造函数复制中使用类型定义的类型的模板
- 将构造函数复制为模板化的成员函数
- 绕过私有复制构造函数/复制赋值C++
- C++通过构造函数复制对象
- 复制构造函数 - 复制C++中的对象
- 将带unique_ptr的类的构造函数复制到作为成员的抽象类