function pointer question

Started by
0 comments, last by fpsgamer 15 years, 11 months ago
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.
Advertisement
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.

This topic is closed to new replies.

Advertisement