Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制继承类的构造函数_C++_Qt_Copy Constructor - Fatal编程技术网

C++ 复制继承类的构造函数

C++ 复制继承类的构造函数,c++,qt,copy-constructor,C++,Qt,Copy Constructor,我试图定义一个类的复制构造函数,但我弄错了。我正试图用这个构造函数来实现一个QGraphicsRectItem的儿子: QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 ) 这里有一些代码 QtL定义的qgraphicsrecitem QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, Q

我试图定义一个类的复制构造函数,但我弄错了。我正试图用这个构造函数来实现一个
QGraphicsRectItem
的儿子:

QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 )
这里有一些代码

QtL定义的qgraphicsrecitem

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())
{}

原因是由于继承,您的
单元格
qgraphicsrecitem
。因此,构造函数的
c
参数也表示
QGraphicsRectItem
,因此您可以使用它的
QGraphicsRectItem::rect()
QGraphicsRectItem::parent()
构造新对象的函数-c的副本

你确定你实际上是从正确的父类继承的吗?行上还有什么写着
类单元格
?这就解决了它:Cell::Cell(const Cell&c):qgraphicsrecitem(c.rect().x(),c.rect().y(),c.rect().width(),c.rect().height()){}有人知道为什么吗?(很长一段时间我不使用我亲爱的C++)您现在正在从复制构造函数调用基类的构造函数