Sign in to follow this  
BreathOfLife

function pointer question

Recommended Posts

OK, Im getting a few unresolved externals for some code, and I cant for the life of me figure out why. My googling didnt find anything I could figure out my situation from. Im running VS 2008 on a vista notebook. In a .h there is this:
typedef struct _input_event
{
	_input_event();
	~_input_event();

	int type;
	int key;

} INEVENT, *LPINEVENT;

and in the matching .cpp:
void (*EventHandler)(LPINEVENT) = NULL;


//in here some code to set a void func that takes a LPINEVENT

	if ( !(old_keystates == new_keystates) )
	{
		LPINEVENT inevent = new INEVENT;
		for (key = 0; key < 256; key++)
		{
			if ( (old_keystates[key] == STATE_UP) && (new_keystates[key] == STATE_DOWN) )
			{
			inevent->type = KEY_PRESS;
			inevent->key = key;
			(*EventHandler)(inevent);
			}
		}
		delete inevent;
	}
}

The two errors Im getting are as follows: 1>SYS_INPUT.obj : error LNK2001: unresolved external symbol "public: __thiscall _input_event::_input_event(void)" (??0_input_event@@QAE@XZ) 1>SYS_INPUT.obj : error LNK2001: unresolved external symbol "public: __thiscall _input_event::~_input_event(void)" (??1_input_event@@QAE@XZ) (naturally and a third telling me there is these 2) I havent used function pointers all that terribly much, but when trying the examples in that "learn c++ in 21 days" they worked fine.

Share this post


Link to post
Share on other sites
The linker is telling you it cannot find the implementation of struct _input_event's constructor or destructor. Either define them inline in the header, or inside one of your cpp files.

[edit]

Also I thought I'd add some points regarding writing idiomatic C++ code. In C++ we do not typedef structs. This is done in C because subsequent declarations of the struct are required to be prefixed with the struct, keyword and this is considered cumbersome. For example:


struct foo
{
....
};

struct foo myFoo;


However in C++ you can omit the struct keyword in subsequent declarations: foo myFoo;

Lastly in C++ we (generally) prefer to use function objects over function pointers. Additionally the boost library provides boost::bind and boost::function, which provide a fantastic function objects implementation.

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