【问题标题】:Forward declaration of a nested class嵌套类的前向声明
【发布时间】:2012-08-08 20:34:41
【问题描述】:

我正在尝试转发 delcare 这个嵌套类,我已经尝试过了,但我没有工作。当我尝试转发声明时,我无法访问私有成员错误,所以我想我做错了什么。

#ifndef PLAYERHOLDER_H
#define PLAYERHOLDER_H

#include <QtCore>
#include <player.h>
#include <datasource.h>

class PLAYERHOLDER
{

private:
class CONTACTMODEL : public QAbstractTableModel
{
public:
    explicit CONTACTMODEL(PLAYERHOLDER* holder);

    int rowCount( const QModelIndex &parent ) const;
    int columnCount( const QModelIndex &parent ) const;
    QVariant data( const QModelIndex &index, int role ) const;
    QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
    void update();


private:
    static PLAYERHOLDER* m_playerHolder;
};

public:
static PLAYERHOLDER* getInstance();
void createPlayer(PLAYER *player);
void updatePlayer(int id);
void deletePlayer(int id);
PLAYER* findPlayer(int id);
void loadPlayers(int teamid);

QAbstractItemModel* model() ;

private:
PLAYERHOLDER();
static PLAYERHOLDER *thePlayerholder;
QHash<int, PLAYER*> playerlist;
DATASOURCE *datasource;
mutable CONTACTMODEL *m_model;
};

#endif // PLAYERHOLDER_H

但我不知道该怎么做,我四处搜索仍然不知道:(是否可以转发声明这个?

【问题讨论】:

标签: c++ forward-declaration


【解决方案1】:

嵌套类型是封闭类型的一部分。就是说不能自己前向声明,但是可以在封装类型的定义中声明,然后在外面定义:

class enclosing {
   class inner;          // Forward declaration
};
// Somewhere else
class enclosing::inner { // Definition
   int x;
};

你不能做的是在封闭类型的定义之外向前声明内部类型:

class enclosing::outer;  // Error

【讨论】:

  • 对不起,按回车键;)我想把它分成 playerholder.h/.ccp 和 contactmodel.h/cpp。但这不是工作,我添加了 CONTACTMODEL 类;给玩家持有者,包括contactmodel.h。 contactmodel.h 中的类看起来像这个类 PLAYERHOLDER::CONTACTMODEL : public QAbstractTableModel。它包括 playerholder.h。它给了我很多错误,其中之一是 PLAYERHOLDER: No class or namespace at the class PLAYERHOLDER:: ... line
  • @user1585758:在我看来你不应该把它分成多个文件,它们都是一个组件的一部分,应该一起管理。如果你真的想划分,playerholder 标头在类定义中只有ContactModel 的声明,并且不会包含contactmodel.h,而contactmodel.h 必须包含playerholder.h访问封闭类型的定义并允许编译器看到您正在定义嵌套类型。同样,我不会这样做,但它是可行的(并且可能需要打破依赖关系)
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
相关资源
最近更新 更多