error: C2248: 'QGraphicsItem::QGraphicsItem

2019-08-28 04:58发布

问题:

I encountered the error described on the post title in Qt.

I have a class call "ball", and it inherits class call "tableItem" which inherits QGraphicsItem. I'm trying to use prototype design pattern, and thus have included a clone method inside the ball class.

Here's my code snippet: For ball header and class

#ifndef BALL_H
#define BALL_H

#include <QPainter>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QtCore/qmath.h>
#include <QDebug>
#include "table.h"
#include "tableitem.h"

class ball : public TableItem
{
public:
    ball(qreal posX, qreal posY, qreal r, qreal VX, qreal VY, table *table1);
    ~ball();


    virtual ball* clone() const;

    virtual void initialise(qreal posX, qreal posY, qreal r, qreal VX, qreal VY);

private:

    table *t;

};

#endif // BALL_H

and the ball class:

#include "ball.h"

ball::ball(qreal posX, qreal posY, qreal r, qreal VX, qreal VY, table *table1):
    TableItem(posX, posY, r, VX, VY),
    t(table1)
{}

ball::~ball()
{}

/* This is where the problem is. If i omitted this method, the code runs no problem! */
ball *ball::clone() const
{
    return new ball(*this);

}

void ball::initialise(qreal posX, qreal posY, qreal r, qreal VX, qreal VY)
{
    startX = posX;
    startY = posY;
    setPos(startX, startY);

    xComponent = VX;
    yComponent = VY;

    radius = r;
}

the tableItem header:

#ifndef TABLEITEM_H
#define TABLEITEM_H

#include <QPainter>
#include <QGraphicsItem>
#include <QGraphicsScene>

class TableItem: public QGraphicsItem
{
public:
    TableItem(qreal posX, qreal posY, qreal r, qreal VX, qreal VY);

    virtual ~TableItem();

    qreal getXPos();
    qreal getYPos();
    qreal getRadius();


protected:
    qreal xComponent;
    qreal yComponent;

    qreal startX;
    qreal startY;
    qreal radius;

};

#endif // TABLEITEM_H

and the tableitem class:

#include "tableitem.h"

TableItem::TableItem(qreal posX, qreal posY, qreal r, qreal VX, qreal VY)
{
    this->xComponent = VX;
    this->yComponent = VY;

    this->startX = posX;
    this->startY = posY;

    this->radius = r;
}

TableItem::~TableItem()
{}
qreal TableItem::getXPos()
{
    return startX;
}

qreal TableItem::getYPos()
{
    return startY;
}

qreal TableItem::getRadius()
{
    return radius;
}

Googling the problem and searching the stackoverflow forum seems to indicate some of the qgraphicsitem constructor or variables are declared private, and thus give rise to this. Some solution indicates the use of smart pointer, but that doesn't seem to work in my case.

Any help is appreciated.

回答1:

Providing your own copy constructor may help.

The default copy constructor tries to copy all data members from your class and its parents.

In your own copy constructor, you can handle copying of the data using the most appropriate way of copying.