c++classincludeforward-declaration

Includes and classes in C++


Well, i have the following problem.. I have main.cpp

#include "serverClass.h"
...

and then in serverClass.h,

#ifndef SERVERCLASS_H_
#define SERVERCLASS_H_
#include <stdio.h>
#include <stdlib.h>
#include "clientThread.h"
....

and in clientThread,

class ClientThread {
private:
serverClass* server;
....

But when it tries to compile ClientThread, it says serverClass isn't a type, because it wasn't compiled yet. I can't avoid including clientThread.h in serverClass. Is there anyway to tell the compiler that there is a type called serverClass that wasn't compiled yet?


Solution

  • You need to forward delcare serverClass:

    class serverClass;
    
    class ClientThread {
    private:
    serverClass* server;
    ....