includes not working (yes I've read all the many posts on includes)

I can't seem to figure out why my includes aren't working. I've read all the articles and many, many posts on this and none of them address my problem. So after a couple of days I finally decided to write a post.

Here's the code for the header and cpp files:

problem.h
#ifndef _STATE_GAME_H_
#define _STATE_GAME_H_
#include "state_Game.h"
#include "state_Menu.h"

class problem {
	private:
	
	public:
		problem();
		void changeState(int k, state_Game *sg);
		var update(state_Game *sg);
};

#endif


problem.cpp
#include "problem.h"
#include "object_Background.h"

class state_Menu;

void problem::changeState(int k, state_Game *sg)
{
	if (sg->getCurrrentState() >= 0)
	{
		sg->clearState();
		sg->setCurrrentState(-1);
	}
	state_Menu* psm;
	sg = dynamic_cast<state_Game*>(psm);
};

var problem::update(state_Game *sg)
{
	sg->update();
};

problem::problem()
{
	state_Game *gameState;
	gameState = new state_Game;
	object_Background *background;
	background = new object_Background;
	changeState(0, gameState); //set to MENU_STATE at first
};


When I compile this I get the following errors:

problem.cpp: In member function ‘void problem::changeState(int, state_Game*)’:
problem.cpp:14: error: cannot dynamic_cast ‘psm’ (of type ‘struct state_Menu*’) to type ‘class state_Game*’ (source is a pointer to incomplete type)


It seems that in the cpp file forward declaring "class state_Menu;" is not being understood by the compiler as referring to the class in state_Menu.h. At least that's what I think it is doing when it says that &psm is of type "struct state_Menu*" and not "class state_Menu*".

Am I right in thinking this?

And if I am, how to get the compiler to recognize the reference?

There is also a problem with the dynamic cast, but without having the proper state_Menu class being referenced it becomes impossible to sort that out.

Can anyone tell me what's wrong here with this code?
Last edited on
Please do your forward declaration in *.h file... and make sure that your
.h and .cpp files are at the same location...
Since I don't use state_Menu in the .h file, it does me no good to do the forward declaration there. I will get state_Menu undeclared errors in the cpp file.

I have tried including the state_Menu.h in the cpp file, but it then gives me state_Menu undeclared errors.

I do understand your remark, and actually did that at first. I was driven to try the forward declaration in the cpp fil precisely because the way you suggest did not work.
Try to do forward declaration in problem.h. you are using class stat_Menu in problem.h file.
Last edited on
No I am not using it in problem.h. I am including the state_Menu header in problem.h because I want to use the class state_Menu in problem.cpp file.

I have tried doing what you suggest already and it doesn't work. I still have gotten the same error:

problem.cpp: In member function ‘void problem::changeState(int, state_Game*)’:
problem.cpp:12: error: cannot dynamic_cast ‘psm’ (of type ‘struct state_Menu*’) to type ‘class state_Game*’ (source is a pointer to incomplete type)
In your problem.h file you are declaring
1
2
void changeState(int k, state_Game *sg);
var update(state_Game *sg);


that's where you are using state_Game...
In problem.h your include guard is:
1
2
#ifndef _STATE_GAME_H_
#define _STATE_GAME_H_ 


What is your include guard in state_Game.h? If it is the same, that's a problem.
http://www.cplusplus.com/forum/articles/10627/ (point 4)
1
2
3
4
5
6
7
8
9
10
11
//problem.h
#pragma once
class state_Game;

//...

//problem.cpp
#include "problem.h"
#include "state_Game.h"
#include "state_Menu.h"
//... 

By the way, your code makes no sense and you are leaking memory
@Hiteshi
I never said I didn't use state_Game in problem.h, I said I didn't use state_Menu. The problem is with state_Menu. Note that the forward declaration in the problem.cpp is for state_Menu NOT state_Game. Please read carefully.

@cire
rest assured that my guard state is not the same in state_Game.h. There are no includes or forward declarations in state_Game.h. It is a very simple class with one setter and one getter.

@ne555
this is a fragment of a more complex cpp & h duo which was created to isolate the problem. It does leak memory a little, but it is not anything but a way of isolating the problem. It is never meant to be anything but a test. ANd the error has nothing to do with lost pointers, etc. You admit that you don't understand the sample or its intent, so why comment? Your suggestion doesn't work, anyway.

Until you supply code that reproduces the problem, all anyone can do is guess.

What we know is that the type state_Menu is not complete when it is encountered in problem::changeState. state_Menu.h is #included in problem.h which is included in problem.cpp. If the definition of state_Menu resides in state_Menu.h there should not be a problem, unless, as I mentioned before, there is some include guard tomfoolery that makes the inclusion effectively a no-op. If the definition of state_Menu does not reside in state_Menu.h, then that is the problem. The compiler needs to see the full definition of state_Menu before problem::changeState is encountered.

But, again, this is just guesswork unless you supply an accurate example that reproduces the problem.
Cire, I do appreciate your response. And I was hoping that the simplified version I posted would be enough to sort out the problem. Since it was not I will post the full version as I have it.

problem.h
#ifndef _STATE_GAME_H_
#define _STATE_GAME_H_
#include "AS3/AS3++.h" // using AS3 var wrapper class
#include "Flash++.h" // using AVM2 sync primitives

// use "ui" AS3 var wrappers which marshall var manipulations to the ui Worker
using namespace AS3::ui;

class state_Game;
class state_Menu;

class problem {
	private:
	
	public:
		problem();
		void changeState(int k, state_Game *sg);
		var update(state_Game *sg);
};

#endif


problem.cpp
#include "problem.h"
#include "state_Game.h"
#include "state_Menu.h"
#include "object_Background.h"

void problem::changeState(int k, state_Game *sg)
{
	if (sg->getCurrrentState() >= 0)
	{
		sg->clearState();
		sg->setCurrrentState(-1);
	}
	state_Menu* psm;
	sg = dynamic_cast<state_Game*>(psm);
};

var problem::update(state_Game *sg)
{
	sg->update();
};

problem::problem()
{
	state_Game *gameState;
	gameState = new state_Game;
	object_Background *background;
	background = new object_Background;
	changeState(0, gameState); //set to MENU_STATE at first
};


state_Game.h
#include "AS3/AS3++.h" // using AS3 var wrapper class
#include "Flash++.h" // using AVM2 sync primitives

// use "ui" AS3 var wrappers which marshall var manipulations to the ui Worker
using namespace AS3::ui;

class state_Game {
	private:
		int current_state;

	public:
		state_Game()
			{
				current_state = -1;
			};
		void makeState()
			{
			};
		void clearState()
			{
			};
		void update()
			{
			};
		int getCurrrentState()
			{
				return current_state;
			};
		void setCurrrentState(int k)
			{
				current_state = k;
			};
};


state_Menu.h
#include "AS3/AS3++.h" // using AS3 var wrapper class
#include "Flash++.h" // using AVM2 sync primitives
#include "state_Game.h"

// use "ui" AS3 var wrappers which marshall var manipulations to the ui Worker
using namespace AS3::ui;

class state_Menu : public state_Game 
{
	public:
		state_Menu()
			{
			};
};


The #includes to action script classes doesn't affect the C++ code, but I include it to explain the var manipulations.

At this point when I compile this I get the following error message:

flascc/sdk/usr/bin/../../usr/lib/stdlibs_abc/crt1_c.o: error: undefined reference to '_main'


and no other error. Is this a problem with the Flascc compiler and should I take it up with Adobe?
You need include guards in `state_{Game,Menu}.h' as you are defining a class there.
Your compiler should give you a `redefinition' error, so yes, issue a bug.

> undefined reference to '_main'
http://cplusplus.com/doc/tutorial/program_structure/


By the way `doesn't work' is not an error message
oh ... ne555 = snap .... take another drink ....
Got it working. No thanks to anyone here.
Topic archived. No new replies allowed.