C++ 错误:C2248:';QGraphicsItem::QGraphicsItem';:无法访问类';QGraphicsItem';
我遇到了Qt中帖子标题上描述的错误 我有一个类调用“ball”,它继承了类调用“tableItem”,它继承了QGraphicsItem。 我试图使用原型设计模式,因此在ball类中包含了一个克隆方法 以下是我的代码片段: 球头和球类C++ 错误:C2248:';QGraphicsItem::QGraphicsItem';:无法访问类';QGraphicsItem';,c++,qt,class,private,qgraphicsitem,C++,Qt,Class,Private,Qgraphicsitem,我遇到了Qt中帖子标题上描述的错误 我有一个类调用“ball”,它继承了类调用“tableItem”,它继承了QGraphicsItem。 我试图使用原型设计模式,因此在ball类中包含了一个克隆方法 以下是我的代码片段: 球头和球类 #ifndef BALL_H #define BALL_H #include <QPainter> #include <QGraphicsItem> #include <QGraphicsScene> #include <
#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
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
谷歌搜索这个问题并搜索stackoverflow论坛似乎表明一些qgraphicsitem构造函数或变量被声明为私有,从而导致了这种情况。
一些解决方案指出使用智能指针,但在我的情况下似乎不起作用
感谢您的帮助 提供自己的副本构造函数可能会有所帮助 默认复制构造函数尝试从类及其父类复制所有数据成员
在您自己的复制构造函数中,您可以使用最合适的复制方式来处理数据的复制。错误在哪一行?我认为如果您可以提供指向
QGraphicsItem
头的链接,这将非常有用,因为错误消息提到类似乎是ball:ball*ball::clone中clone方法的返回语句()const{return new ball(*this);}但是Qt表示错误来自tableItem头的末尾..因此我不确定..Qt error将我指向QGraphicsItem头中的这一行:private:Q_DISABLE_COPY(QGraphicsItem)…相关问题:是的,问题似乎是复制构造函数。我提供了自己的复制构造函数,它似乎可以工作。谢谢:)
#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
#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;
}