问题描述
我遇到了在Qt的帖子标题上描述的错误。
I encountered the error described on the post title in Qt.
我有一个类调用ball,它继承类调用tableItem,继承QGraphicsItem 。
我试图使用原型设计模式,因此在球类中包含了一个克隆方法。
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
和球类:
#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标题:
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
和tableitem类别:
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;
}
搜索问题并搜索stackoverflow论坛似乎表明一些qgraphicsitem构造函数或变量被声明为私有的,因此产生这种情况。
有些解决方案表示使用智能指针,但是在我的情况下似乎不工作。
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.
任何帮助是赞赏。
推荐答案
提供自己的复制构造函数可能会有帮助。
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.
这篇关于错误:C2248:'QGraphicsItem :: QGraphicsItem':不能访问私人成员声明在类'QGraphicsItem'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!