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

问题描述:

我遇到了Qt中帖子标题中描述的错误。错误:C2248:'QGraphicsItem :: QGraphicsItem':无法访问在'QGraphicsItem'类中声明的私有成员'

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

这里是我的代码片段: 对于球头和类

#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 

和表项类:

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

谷歌搜索的问题和搜索的*论坛似乎表明一些qgraphicsitem的构造函数或变量被宣布为私人,从而引发这一点。 一些解决方案表示使用智能指针,但这似乎不适用于我的情况。

任何帮助表示赞赏。

+0

哪一行是错误? – 2013-05-06 03:46:04

+0

我认为这将是有用的,如果你可以提供链接到'QGraphicsItem'头,因为错误消息提到类 – 2013-05-06 03:49:17

+0

似乎是克隆方法在球中的返回语句: ball * ball :: clone()const {返回新球(* this); } 但Qt的指示错误来自表项header..thus结束我不知道.. – ImNoob 2013-05-06 03:50:04

提供您自己的拷贝构造函数可能会有帮助。

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

在您自己的拷贝构造函数中,您可以使用最适当的拷贝方式处理数据的拷贝。

+0

是的,问题似乎是复制构造函数。我提供我自己的拷贝构造函数,它似乎work.thanks :) – ImNoob 2013-05-06 04:47:33