# An #include problem

This topic is 4788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i am getting error messages of undeclared identifiers, but I can't figure out where I'm going wrong. Here's the code: //State.cpp #pragma once #include "game_shooter.h" class State { private: GAME_Shooter * game; //a pointer to a game object ... //GAME_Shooter.cpp #pragma once #include "state.h" class GAME_Shooter { private: State * State; //a pointer to a state object ... Objects of these types of classes call each others functions. There are .h files for the .cpp files. I am working with visual studio .net 2003. (C++) Here are some examples of the error messages: GAME_Shooter.h(28): error C2061: syntax error : identifier 'State' GAME_Shooter.h(11): error C2143: syntax error : missing ';' before '*' GAME_Shooter.h(11): error C2501: 'GAME_Shooter::state' : missing storage-class or type specifiers Now this is a very irritating thig, because you should be able to use the class identifier after you have included it in the file, right? I would appreciate any ideas.

##### Share on other sites
Looks like you're declaring your State class in the .cpp file, but trying to include state.h in GAME_Shooter.cpp.

state.h does not contain the declaration of "State", so the compiler has no idea what it is.

Typically one would declare the classes in their respective .h files, and place the definition in the .cpp.

Also, there's no point in putting #pragma once in the .cpp. At least, not as I understand it.

eg:
// State.h#pragma once#ifndef STATE_H#define STATE_Hclass GAME_Shooter; // forward ref to GAME_Shooter, so you don't need to include that .hclass State{private:  GAME_Shooter *game;//...};   #endif// State.cpp #include "State.h"// ... and place the class method definitions here.// GAME_Shooter.h#pragma once#ifndef GAME_SHOOTER_H#define GAME_SHOOTER_Hclass State; // forward ref to the State class, so you don't need to include the .hclass GAME_Shooter {private:  State* state; // ....};#endif// GAME_Shooter.cpp#include "GAME_Shooter.h"#include "State.h"// ... and place GAME_Shooter definitions here.

Also, it's considered (by many) to be bad design when you have two classes which reference each other. ie: ClassA contains a pointer to ClassB, and ClassB contains a pointer to ClassA. This is known as "strong coupling" and should be avoided.

##### Share on other sites
Hint: you don't need to write "#pragma once" in *.cpp files

##### Share on other sites
The way you probably want it setup is:

// State.h// Foward declare the GAME_shooter classclass GAME_shooter;class State{    GAME_shooter * shooter;}// State.cpp#include "State.h"#include "game_shooter.h"

// GAME_shooter.h// Foward delcare the State classclass State;class GAME_shooter{    State * state;}// GAME_shooter.cpp#include "GAME_shooter.h"#include "State.h"

The foward declarations allow you to add pointers to classes that you haven't yet fully declared. Then in the cpp files you include the real header files for those other classes to get the class definitions and you can use the pointers as normal.

Alan

##### Share on other sites
this is just a guess because we don't have all the code but does game_shooter.h declare a class or struct that is not terminated with a ';'? for example:

// foo.hstruct foo { }  // note! missing a ';'// foo.cpp#include "foo.h"struct bar { foo * f; };

##### Share on other sites
Oops, gave you wrong info, the files were .h and not .cpp, my mistake.

But the thig that really pisses me off is that I have succeeded including and using classes normally in other files, like this interface class header here:

#pragma once
#include "weapon.h"

class IArmed
{
private:
Weapon * weapon;
...

I mean, come on! This IDE is just playing games with me or something! (Sorry, I'm just losing my nerves here, mabe I'd better get some rest.)

mgarriss: The files should be ok.

##### Share on other sites
My guess (without seeing the full source for State.h) would also be that you're missing a semicolan somewhere.

When all else fails though, start commenting stuff out 'till it does compile :P It's an act of desperation, I know.. but it does help narrow things down sometimes.

##### Share on other sites
Here's the code:

GAME_Shooter.h
#pragma once#include "state.h"class GAME_Shooter{private:	State * state;public:		GAME_Shooter(void);	~GAME_Shooter(void);	int Init(void *parms);	int Main(void *parms);	int Shutdown(void *parms);		int setState(State * state)	{		delete this.state;		this.state = state;	}};

#pragma once#include "game_shooter.h"class State{private:	GAME_Shooter * game;public:	State(void);	virtual ~State(void);	virtual void run() = 0;};

The class design is in it's baby stage. I just would like to be able to include things normally...
vs.net 2003 is a pritty complex ide. Could there be some clas properties, settings, etc. that I'm not paying attention to?

##### Share on other sites
Warp,

I noticed something which might be your problem. In GAME_Shooter.h you include "state.h" but then you immediately follow it by forward declaring class "State".

The reason you're doing this is because of the re-enterant header files. However, you've got the forward declerations in the wrong place. Class GAME_Shooter actually USES class State inside of the header file, so it must know about it when it attempts to compile.

State, however only stores a pointer to GAME_Shooter in the header file, it does not actually use it.

Solution: Do not forward declare State in GAME_Shooter.h. Instead, forward declare GAME_Shooter inside of State.h and remove the line:

#include "game_shooter.h"

from within state.h

so your code looks like this:

State.h
--------
#pragma once
class GAME_Shooter;

class State
{
...
}

GAME_Shooter.h
----------
#pragma once
#include "state.h"

class GAME_Shooter
{
...
}

##### Share on other sites
Thanks jwalsh, but I was actually just testing that class prototype exhibiting there (it didn,t work though, got some strange errors, like
Shooter error LNK2019: unresolved external symbol "public: __thiscall GAME_Shooter::GAME_Shooter(void)" (??0GAME_Shooter@@QAE@XZ) referenced in function _WinMain@16
jeesh!)
I have edited that out now.
I think I just have to make a new project and try to remake some files, maybe it'll help.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631722
• Total Posts
3001892
×