2013-12-10 3 views
0

Я пытаюсь определить конструктор копирования класса, но я ошибаюсь. Я пытаюсь сделать сына QGraphicsRectItem с помощью этого конструктора:конструктор копирования унаследованного класса

QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0) 

здесь код

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' 

спасибо

+1

Вы действительно наследуете от правого родительского класса? –

+0

Что еще находится на линии, которая говорит 'class Cell'? –

+0

это разрешимо: Cell :: Cell (const Cell & c): QGraphicsRectItem (c.rect(). X(), c.rect(). Y(), c.rect(). Width(), c.rect() .height()) {} Кто-нибудь знает почему? (долгое время я не являюсь моим дорогим C++) – Giquo

ответ

1

Вы должны сделать свой конструктор копирования следующим образом:

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.

Смежные вопросы