I'm trying to create some kind of extremely simple event system in C++. I have these files:
Events.h
#pragma once
#include <vector>
#include <functional>
using namespace std;
template <typename... Args>
class Event
{
public:
typedef function<void(Args...)> HandlerFunc;
void Add(HandlerFunc handler);
void operator()(Args... params);
private:
vector<HandlerFunc>* handlers = new vector<HandlerFunc>;
};
Events.cpp
#include "Events.h"
template <typename... Args>
void Event<Args...>::Add(HandlerFunc handler)
{
handlers->push_back(handler);
}
template <typename... Args>
void Event<Args...>::operator()(Args... params)
{
for (const auto& handler : handlers)
{
handler(params...);
}
}
main.cpp
#include <iostream>
#include "Events.h"
using namespace std;
void handler(int x)
{
cout << "HANDLER: " << x;
}
int main()
{
Event<int> evt;
evt.Add(handler);
evt(69);
return 0;
}
But I get the following errors:
LNK2001 unresolved extern symbol "public: void __cdecl Event::operator()(int)" (??R?$Event@H@@QEAAXH@Z)
LNK2001 unresolved extern symbol "public: void __cdecl Event::Add(class std::function<void __cdecl(int)>)" (?Add@?$Event@H@@QEAAXV?$function@$$A6AXH@Z@std@@@Z)
LNK1120 2 unresolved externs
Why is that?