错误: C2248: 'QGraphicsItem::QGraphicsItem': 无法访问类 'QGraphicsItem' 中声明的私有成员
error: C2248: 'QGraphicsItem::QGraphicsItem' : cannot access private member declared in class 'QGraphicsItem'
我在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构造函数或变量被声明为私有,从而导致了这个问题。一些解决方案表明使用智能指针,但在我的情况下似乎不起作用。
任何帮助都是感激的。
提供自己的复制构造函数可能会有所帮助。
默认复制构造函数尝试从类及其父类复制所有数据成员。
在您自己的复制构造函数中,您可以使用最合适的复制方式来处理数据的复制。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 错误: C2248: 'QGraphicsItem::QGraphicsItem': 无法访问类 'QGraphicsItem' 中声明的私有成员