A small challenge

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
yaten
Posts: 41
Joined: Tue Nov 16, 2010 4:57 pm
Location: /home/yaten on a server somewhere East of Asia
Contact:

A small challenge

Post by yaten »

Hi masters,

I have a very small problem, I bet others would have already thought of a better solution to this, so I'll try my luck and post my problem. Sorry, this isn't Irrlicht related but I don't know where else I could post this so I posted it here.

Without much ado, here's the code (greatly condensed):

Code: Select all

namespace ykIrr
{
	typedef int (*flowControlFunction)(ykIrr::tIrrlichtEngine, ykIrr::sceneClass, flowControl );

	class flowControl
	{
		private:
			typedef struct tState
			{
				flowControlFunction f;	// function
			} tState;
			tState S;

		public:
			int addState( flowControlFunction f)
			{
				S.f = f;
				return 1;
			}
	};
}

as you may have noticed, the part

Code: Select all

	typedef int (*flowControlFunction)(ykIrr::tIrrlichtEngine, ykIrr::sceneClass, flowControl );
is the problem. This is because I used flowControl which isn't declared yet. The problem is, I need to call functions of flowControl from within the function that was dynamically called by flowControl. Something like this:

Code: Select all

namespace Menu
{
	int Main(ykIrr::tIrrlichtEngine I, ykIrr::sceneClass SC, ykIrr::flowControl ) 
	{ 
		//fC.setState("main");
		return 1; 
	}
}

int main()
{
		ykIrr::flowControl fC;
		fC.addState( &Menu::Main );
}
I hope I was able to convey my intention, i.e. to call a function that is from within flowControl class from within Menu::Main. Anybody got a working solution for that challenging task? I hope you could enlighten me.

Thanks and more power to everyone! ^_^ If what I've done (i.e. post this problem here) is wrong, please forgive me and kindly ignore or delete this topic.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Just make a forward declaration for that class.
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

You can't use a type before the compiler knows which size it has. And the compiler always works from top to bottom. This is the problem.

The way around that is to work with pointers or references instead, as those all have the same size, so the compiler can handle them. You still have to tell him ahead that the type will be declared later. You do that with a forward declaration. Like:

Code: Select all

// forward declaration to tell the compiler that flowControl is of type class.
class flowControl; 

// now you can use flowControl as reference
typedef int (*flowControlFunction)(ykIrr::tIrrlichtEngine, ykIrr::sceneClass, flowControl& );

// your real class can follow later on
class flowControl 
{
};
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
yaten
Posts: 41
Joined: Tue Nov 16, 2010 4:57 pm
Location: /home/yaten on a server somewhere East of Asia
Contact:

Post by yaten »

hybrid wrote:Just make a forward declaration for that class.
omg, I knew my problem would be so trivial to you master hahahaha ^_^ how stupid of me to forgot about forward declarations.

I couldn't thank you enough for enlightening me ^_^ I should have done this a week ago, I've been stuck here for a week and you just solved it so easily. Thanks again! ^_^
yaten
Posts: 41
Joined: Tue Nov 16, 2010 4:57 pm
Location: /home/yaten on a server somewhere East of Asia
Contact:

Post by yaten »

CuteAlien wrote:You can't use a type before the compiler knows which size it has. And the compiler always works from top to bottom. This is the problem.

The way around that is to work with pointers or references instead, as those all have the same size, so the compiler can handle them. You still have to tell him ahead that the type will be declared later. You do that with a forward declaration. Like:

Code: Select all

// forward declaration to tell the compiler that flowControl is of type class.
class flowControl; 

// now you can use flowControl as reference
typedef int (*flowControlFunction)(ykIrr::tIrrlichtEngine, ykIrr::sceneClass, flowControl& );

// your real class can follow later on
class flowControl 
{
};
Sir, this is an interesting approach ^_^ I'd like to try this too ^_^
Thanks so much for the input ^_^ I hope I could return the favor someday ^_^
Post Reply