0

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?

  • 2
    OT: You almost *never* need a pointer to a container. Why is `handlers` a pointer? Why do you create it dynamically? And when do you `delete` it? – Some programmer dude Dec 31 '22 at 09:24

0 Answers0