I'm trying to define the copy constructor of a class but I'm mistaking. I'm trying to do a son of QGraphicsRectItem
using this constructor:
QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 )
here some code
QGraphicsRectItem defined by QtL
QGraphicsRectItem( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 )
Cell.h, son's class:
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
...
}
error says:
/../../../../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'
thank you
You need to make your copy constructor as follows:
Cell::Cell(const Cell &c)
:
QGraphicsRectItem(c.rect().x(), c.rect().y(),
c.rect().width(), c.rect().height(),
c.parent())
{}
The reason is that your Cell
class is a QGraphicsRectItem
because of inheritance. Thus c
argument of the constructor is also representing QGraphicsRectItem
, so you can use its QGraphicsRectItem::rect()
and QGraphicsRectItem::parent()
functions to construct new object - the copy of c
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.