Problem with function pointer

Started by
2 comments, last by Emmanuel Deloget 17 years, 9 months ago
Hello everyone. I have a bit of a problem with passing function pointers as an argument to a function. That is, the declaration is no problem, but when using the 'passed' function within the function it gives me an error saying: error C2064: term does not evaluate to a function The entire function can be seen below, but before I'll copy 'n paste the code I'll put it in a context as to explain what I'm trying to do. Basically I'm adding functions to a "3d engine" as I continue learning Direct3D. Everytime I've learned something new I think of how I would ease the process and add it to my class. The function below is the InitGameLoop function, which will start the game loop and takes a function pointer as argument. The function the user provides is the 'loop content'.

int u3D::IUniverse3DInterface::U3DInitGameLoop ( bool (*LoopFunc), MSG ** u_msg )
{
	// Initialize windows MSG struct
	MSG msg;	

	// Start the loop
	while ( true ) {

		// Check for windows messages
		if ( PeekMessage ( &msg, NULL, 0, 0, PM_REMOVE ) > 0 )
		{
			// If the user defined the wish to receive the message as well
			if ( u_msg != 0 ) {
				*u_msg = &msg;
			}

			TranslateMessage ( &msg );
			DispatchMessage ( &msg );

		} else {

			// Do the specified game loop
			if ( LoopFunc() == false ) {  // This gives the error
				break;
			}

		}
	}
	
	return msg.wParam;
}

I hope this is enough information, if not, I'll gladly provide more ;-)
Advertisement
I think you need an extra pair of parentheses on the end: bool (*LoopFunc)()
Ahh, thanks, you are absolutely right and it worked fine :-) In addition - do you always have to add those parentheses when you're dealing with function pointers, or is it just in specific cases like this one?
Quote:Original post by rogierpennink
Ahh, thanks, you are absolutely right and it worked fine :-) In addition - do you always have to add those parentheses when you're dealing with function pointers, or is it just in specific cases like this one?


The parentheses tells the compiler that the type is a function pointer that has a variable number of parameters. Without the parenthesis, you wouldn't be able to specify the parameters types, and the compiler would not be able to ensure type safety.

Regards,

This topic is closed to new replies.

Advertisement