0

I am trying to forward delcare this nested class, I already tried it but i didnt work. When i try to forward declare i get cant acces private member errors, so I guess i am doing something wrong.

#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

But i dont know how to do it, i searched around and still dont know it :( Is it possible to forward declare this?

digga
  • 103
  • 2
  • 12
  • 1
    possible duplicate of [Forward declaration of nested types/classes in C++](http://stackoverflow.com/questions/951234/forward-declaration-of-nested-types-classes-in-c) – TobiMcNamobi Nov 04 '14 at 10:01

1 Answers1

12

The nested type is part of the enclosing type. That means that it cannot be forward declared by itself, but it can be declared in the definition of the enclosing type, and then defined outside:

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

What you cannot do is forward declare the inner type outside of the definition of the enclosing type:

class enclosing::outer;  // Error
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Sry pressed enter;) I want to split it into playerholder.h/.ccp and contactmodel.h/cpp. But it isnt wokring, i added class CONTACTMODEL; to the playerholder, inculded the contactmodel.h. The class in contactmodel.h looks like this class PLAYERHOLDER::CONTACTMODEL : public QAbstractTableModel. And it includes the playerholder.h. It gives me many error, one of them is PLAYERHOLDER: No class or namespace at the class PLAYERHOLDER:: ... line – digga Aug 08 '12 at 20:53
  • @user1585758: In my opinion you should not divide it in multiple files, both of them are part of a single component and should be managed together. If you really want to divide, the playerholder header would have just the declaration of `ContactModel` inside the class definition and *would not* include contactmodel.h, rather contactmodel.h will have to include playerholder.h to access the definition of the enclosing type and allowing the compiler to see that you are defining a nested type. Again, I would not do it, but it is feasible (and might be needed to break dependencies) – David Rodríguez - dribeas Aug 08 '12 at 20:56