gpt4 book ai didi

c++ - 错误 : C2248: 'QGraphicsItem::QGraphicsItem' : cannot access private member declared in class 'QGraphicsItem'

转载 作者:行者123 更新时间:2023-11-28 07:37:48 24 4
gpt4 key购买 nike

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

我有一个类调用“ball”,它继承类调用“tableItem”,后者继承了 QGraphicsItem。我正在尝试使用原型(prototype)设计模式,因此在 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;
}

tableItem 标题:

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

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

感谢任何帮助。

最佳答案

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

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

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

关于c++ - 错误 : C2248: 'QGraphicsItem::QGraphicsItem' : cannot access private member declared in class 'QGraphicsItem' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com