windy401 windy401 - 1 year ago 122
C++ Question

variable of field declared void error when trying to use object of custom class

I have this header file in my project:


#include "utilities.h"
#include "textOutput.h"
#include "game.h"

class Player

int score;
int poX, posY;


And then in another header file I have

#include <commonincludes.h>

void turn(Player *currplayer, int mapSize);

and matching function in it's own cpp file. I'm getting a variable of field declared void error at the line of the function prototype, and an error saying 'Player' was not declared in this scope despite including the header file with it. Looking through a bunch of other threads here about the error I haven't found one that was isn't for code all in one file or caused by some error I've ruled out. I know I did something like this before and got it to work but I can't figure out what I'm doing differently this time around.

I've also that moving the class definition (copy paste so I know I'm not changing anything with the class definition) to the header file "utilities.h" which is included in the above header file makes it work...

If that's not enough to go off of let me know and I'll pull more to include.

Answer Source

You have circular header inclusion. game.h includes commonincludes.h and commonincludes.h includes game.h. Your include guards break the infinite inclusion cycle, but they break it at the point where Player remains undeclared at the point of turn declaration.

Judging by what you posted so far, there's no need for #include "game.h" in commonincludes.h. What it is doing there?

P.S. Use #include "..." for including your own header files. The #include <...> syntax is for standard headers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download