Sign in to follow this  
TwilightWolf

Passing a pointer to a member function

Recommended Posts

TwilightWolf    122
I'm writing a DirectX Window class for use in some framework provided to me. So far it seems to be going fairly well. Except I have come across an annoying error that I cannot get past. So far I have the following code
void D3DWindow::Run()
{
	if(!InitD3D())
	{
		::MessageBox(0, "InitD3D() - FAILED", 0, 0);
	}
	
	if(!Setup())
	{
		::MessageBox(0, "Setup() - FAILED", 0, 0);
	}
	
        //!!!!!!!!!!!!!!!!
	EnterMsgLoop( Display ); 
	
	Cleanup();
	
	Device->Release();
}
My problem is that the display function is also a member of that class, and for the life of me I cannot compile it successfully. The error I recieve is: Error 5 error C3867: 'D3DWindow::Display': function call missing argument list; use '&D3DWindow::Display' to create a pointer to member g:\directxframework\d3dwindow.cpp 286 Of course the suggestion offered does not work. Can anyone help? [Edited by - TwilightWolf on May 4, 2009 11:11:23 AM]

Share this post


Link to post
Share on other sites
MJP    19756
You can't use a pointer to a member function as if it were a pointer to free function. See the section in C++ FAQ Lite for more details.

There are several common techniques in use for making Win32 message handling play nice with C++ classes, the most common of which is to store a pointer to your class instance in GWLP_USERDATA.

Also...FYI you can format blocks of code on the forums here by using the "code" or "source" tags. See the FAQ.

Share this post


Link to post
Share on other sites
TwilightWolf    122
Sorry, I'm not sure how what you've posted relates to my problem. Or what changes I should make.

EDIT

Ok I have tried something after reading the C++ FAQ Lite section.

I tried to declare a global instance of the class, and create a wrapper for the display function like so:

[source lang = "cpp"]
D3DWindow * Window;

bool DisplayWrapper()
{
return Window->Display(Window->GetDeltaTime());
}



and replaced the "Display" found in the above code with "DisplayWrapper".

Still not cracked though. The error I'm getting know is:

cannot convert parameter 1 from 'bool (__cdecl *)(void)' to 'bool (__cdecl *)(float)'

I can't see how it could think I'm passing a void into the function. GetDeltaTime returns a float of course.

Share this post


Link to post
Share on other sites
MJP    19756
It looks like your EnterMessageLoop function is supposed to take a pointer to function that returns bool and accepts a float parameter, but you're sending it a pointer to a function that returns bool and takes no parameters (that's what the "void" means).

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by TwilightWolf
cannot convert parameter 1 from 'bool (__cdecl *)(void)' to 'bool (__cdecl *)(float)'

I can't see how it could think I'm passing a void into the function. GetDeltaTime returns a float of course.


1) There is no such thing as "a void".

2) It's not complaining about what you're passing into the function; it's complaining about the type of the function. This should be evident from the fact that the error points to the line with the EnterMsgLoop() call.

'bool (__cdecl *)(void)' means "a pointer to a free function that accepts no arguments (i.e., 'void') and returns a bool".

'bool (__cdecl *)(float)' means "a pointer to a free function that accepts a float and returns a bool".

These are clearly not the same type of thing.

The point is that EnterMsgLoop() registers your function with the API, and the API will call the function you provide and takes responsibility for passing a float to it. (I.e., it measures the delta-time for you, and provides that information to your function.) Therefore, the function (wrapper) you provide must accept that float.

Share this post


Link to post
Share on other sites
TwilightWolf    122
Thanks Zahlman. I'll make adjustments tomorrow.

Forgive me for being a newbie though. I've only been writing in C++ since last September. Still, I've learnt something new today.



[Edited by - TwilightWolf on May 5, 2009 5:53:54 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this