error: C2248: 'QGraphicsItem::QGraphicsItem' : cannot access private member declared in class 'QGraphicsItem'

这一生的挚爱 提交于 2019-12-12 03:54:37

问题


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.



来源:https://stackoverflow.com/questions/16391669/error-c2248-qgraphicsitemqgraphicsitem-cannot-access-private-member-decl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!