错误: C2248: 'QGraphicsItem::QGraphicsItem': 无法访问类 'QGraphicsItem' 中声明的私有成员

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

本文关键字:QGraphicsItem 声明 成员 C2248 错误 访问      更新时间:2023-10-16

我在Qt中遇到了帖子标题上描述的错误。

我有一个类调用"ball",它继承了类调用"tableItem",继承了QGraphicsItem。我正在尝试使用原型设计模式,因此在ball类中包含了一个克隆方法。

下面是我的代码片段:对于球头和类
#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

和球类:

#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;
}

表项标题:

#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

和tableitem类:

#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;
}

google这个问题和搜索stackoverflow论坛似乎表明一些qgraphicsitem构造函数或变量被声明为私有,从而导致了这个问题。一些解决方案表明使用智能指针,但在我的情况下似乎不起作用。

任何帮助都是感激的。

提供自己的复制构造函数可能会有所帮助。

默认复制构造函数尝试从类及其父类复制所有数据成员。

在您自己的复制构造函数中,您可以使用最合适的复制方式来处理数据的复制。