Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

boost::function won't link [fixed]

Started by thedustbustr Dec 20, 2004 at 1:32 AM 2 replies 1k views
Original Post
thedustbustr
thedustbustr
I'm working on an event handling framework. It won't link. I don't expect it to run, this code is incomplete. The event manager

#define evmgr EventManager::GetSingleton()
typedef boost::function<void (Event e)> Handler;

class EventManager : public System, public Singleton<EventManager>
{
private:
	std::map<std::string, std::vector<Handler> > handlers;
	std::list<Event> events;
public:
	void add_handler(std::string type, Handler h);
	void handle_event(const Event e);
	void handle_event_now(const Event& e); //ignore time delay requests

	bool init();
	bool tick();
	bool exit();

	EventManager();
	~EventManager();

};


void EventManager::add_handler(std::string type, Handler h)
{
	handlers[type].push_back(h); //register handler with handler list
}
void EventManager::handle_event_now(const Event& e)
{
	for(std::vector<Handler>::iterator i=handlers[e].begin();
		i != handlers[e.type].end(); ++i)
	{
		(*i)(e); //call the handler, passing the event as an argument
	}
}

EventManager::handle_event(const Event e)
{ events.push_back(e); }

bool EventManager::tick() 
{
	double time=clock.GetTime();
	for(std::list<Event>::iterator i=events.begin();
		i!=events.end(); )
	{
		if ((*i).time >= time) 
		{
			handle_event_now((*i));
		    delete (*i); //fixme is this right
			assert(false); //fix iterator
		}
		else ++i;

	}

	return true;
}


And the line causing linker error:

bool Input::init() 
{
    Handler on_keypress; //empty function object
    evmgr.add_handler("key", on_keypress);
    return true;
}
error LNK2019: unresolved external symbol "public: void __thiscall EventManager::add_handler(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class boost::function<void __cdecl(class Event),class std::allocator<void> >)" (?add_handler@EventManager@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$function@$$A6AXVEvent@@@ZV?$allocator@X@std@@@boost@@@Z) referenced in function "public: virtual bool __thiscall Input::init(void)" (?init@Input@@UAE_NXZ)


I really don't want to use function pointers. What did I do wrong? [Edited by - thedustbustr on December 20, 2004 2:58:24 AM]
idadesub
idadesub
I'm guessing by that error message that you're using visual studio. What version are you using? With 6.0, and also .net, there are some issues occationally using the boost library, so you may need to use the compatibility format:

typedef boost::function1<void, Event> Handler;


Also, I would suggest trying this code:

void blah(Event e){}bool Input::init() {    evmgr.add_handler("key", blah);    return true;}


This theoretically should link.

-e
thedustbustr
thedustbustr
I'm using 7.1, which should support the preferred syntax (according to the docs..)
I tried your code and it did not link.
input.obj : error LNK2019: unresolved external symbol "public: __thiscall Event::~Event(void)" (??1Event@@QAE@XZ) referenced in function "void __cdecl blah(class Event)" (?blah@@YAXVEvent@@@Z)input.obj : error LNK2019: unresolved external symbol "public: void __thiscall EventManager::add_handler(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class boost::function<void __cdecl(class Event),class std::allocator<void> >)" (?add_handler@EventManager@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$function@$$A6AXVEvent@@@ZV?$allocator@X@std@@@boost@@@Z) referenced in function "public: virtual bool __thiscall Input::init(void)" (?init@Input@@UAE_NXZ)dist/katana4.exe : fatal error LNK1120: 2 unresolved externals


Using the portable syntax yielded the same two errors in link with your code.
thedustbustr
thedustbustr
Found my error, coder stupidity :) In my excitement to see if my code was right I forgot to create an EventManager instance. sorry to waste your time

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.