# i give up...callback help

## Recommended Posts

Just wondering if anyone can tell me what exactly I'm doing wrong, since I believe I copied it correctly straight out of the book. This is not all of my code, but the area with the problem. input.cpp
#include "input.h"

int CInputDriver::EnumJoysticks()
{
if (!lpdi_driver)
return 0;

GUID dummy_return;

if (FAILED(lpdi_driver->EnumDevices(DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
&dummy_return,
DIEDFL_ATTACHEDONLY)))
return 0;

return 1;
}

bool CALLBACK CInputDriver::EnumJoysticksCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID guid_ptr)
{
joysticks.push_back(*lpddi);
}


input.h
class CInputDriver
{
private:
LPDIRECTINPUT8 lpdi_driver;

vector<DIDEVICEINSTANCE> joysticks;

bool CALLBACK EnumJoysticksCallback(LPCDIDEVICEINSTANCE, LPVOID);

public:
CInputDriver();
~CInputDriver();

int EnumJoysticks();
}


Error: c:\Documents and Settings\Blake Young\My Documents\Visual Studio Projects\Bosco\input.cpp(85) : error C2664: 'IDirectInput8A::EnumDevices' : cannot convert parameter 2 from 'bool (LPCDIDEVICEINSTANCE,LPVOID)' to 'LPDIENUMDEVICESCALLBACKA' None of the functions with this name in scope match the target type

##### Share on other sites
When registering a callback function that is a class member that function must be declared as static (in other words DirectX needs to be able to call the function without an object).

##### Share on other sites
Well, I made the change as you suggested, but not only does that error remain, but a new one is introduced.

input.cpp
#include "input.h"int CInputDriver::EnumJoysticks(){	if (!lpdi_driver)		return 0;	GUID dummy_return;	if (FAILED(lpdi_driver->EnumDevices(DI8DEVCLASS_GAMECTRL, 				 EnumJoysticksCallback, 				 &dummy_return, 				 DIEDFL_ATTACHEDONLY)))		return 0;	return 1;}bool CALLBACK CInputDriver::EnumJoysticksCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID guid_ptr){	joysticks.push_back(*lpddi);}

input.h
class CInputDriver{private:	LPDIRECTINPUT8 lpdi_driver;	vector<DIDEVICEINSTANCE> joysticks;	static bool CALLBACK EnumJoysticksCallback(LPCDIDEVICEINSTANCE, LPVOID);	public:		CInputDriver();		~CInputDriver();		int EnumJoysticks();}

c:\Documents and Settings\Blake Young\My Documents\Visual Studio Projects\Bosco\input.cpp(85) : error C2664: 'IDirectInput8A::EnumDevices' : cannot convert parameter 2 from 'bool (LPCDIDEVICEINSTANCE,LPVOID)' to 'LPDIENUMDEVICESCALLBACKA'
None of the functions with this name in scope match the target type
c:\Documents and Settings\Blake Young\My Documents\Visual Studio Projects\Bosco\input.cpp(93) : error C2228: left of '.push_back' must have class/struct/union type
type is ''

##### Share on other sites
Well I see a few problems in your design

1) The function EnumJoysticksCallback must be declared within the public scope of the class. Think of it as a global function because of the fact that is declared static. Remember that private class data can only be accessed through public accessors and thus DirectX won't be able to access it.

2) You cannot use member variables within static member functions unless they themselves are static.

Also remember that the C type BOOL is different from the C++ type bool and the callback function pointer is defined as:

BOOL CALLBACK DIEnumDevicesCallback(  LPCDIDEVICEINSTANCE lpddi,    LPVOID pvRef  );

##### Share on other sites
Thanks for the advice. It looks like I'm going to have to rethink my overall design. Despite the fact that I do quite a bit of programming, these areas are ones I don't normally work in. I think I need to dust off some of my old C++ manuals.

Now that you mention it, the whole situation with BOOL/FALSE/TRUE and bool/false/true has always sorta confused me, but since the code always seemed to work fine both ways I never paid much attention to it. Are the capitalized versions C and the lowercase C++? What exactly is the underlying difference? I would think one would simply be typedef'd to the other, but I suppose not.

##### Share on other sites
Quote:
 Original post by StonieJAre the capitalized versions C and the lowercase C++? What exactly is the underlying difference? I would think one would simply be typedef'd to the other, but I suppose not.
C and C++ originally did not include Boolean types, so Microsoft created one for the Win16 API, which propagated to Win32 and, presumably, will propagate to Win64 (I've had no motivation to check, given that you can't actually run a Win64 platform yet), which was a typedef to an integral type. This is the BOOL type.

With the '97 and '99 revisions of the C++ and C standards, respectively, the two languages finally obtain standardized boolean types, bool in both languages. Because Boolean evaluations have always existed in C and C++ (the type of a == a was an inaccessible Boolean), and because automatic type promotions for a number of types exist, you can seamlessly replace bool for BOOL and vice versa. Remember, any non-zero value (including negative values) is evaluated as true in C and C++.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628328
• Total Posts
2982099

• 22
• 9
• 9
• 13
• 11