0

Introduction

I'm new to C++, keep that in mind as i try to explain my problem.

i'm making a game in C++ to help me understand the language better, and while i was making this project i decided that i needed events(like the C# events) to help me with this project, and after some researched i found out that C++ doesn't have a event handler system similar to the C# one, so i tried to do something that worked in the same way as the C# event handler.

In the Main.cpp file i created this:

typedef void(*KeyPressedFunction)(sf::Keyboard::Key);
std::vector<KeyPressedFunction> keyPressedEvent;
int main()
{
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::KeyPressed:
                for (KeyPressedFunction func : keyPressedEvent)
                {
                    func(event.key.code);
                }
                break;
            }
        }
    }
}

The idea is that every class that wanted to know when the key was pressed was going to subscribe to the event(push_back to the keyPressedEvent vector) and when a key gets pressed every system would have that information.

The Problem

After i tested the system in the main.cpp file (creating a function and pushing back to the keyPressedEvent vector) i noticed that it was working fine, the test functions that i created were being called correctly, so i decided to implement in the rest of the system, which means...in other translation units.

This is what i did:

  • Pacman.h File:

    typedef void(*KeyPressedFunction)(sf::Keyboard::Key);
    extern std::vector<KeyPressedFunction> keyPressedEvent;
    
    class Pacman
    {
    public:
        Pacman(float x, float y);
        void OnKeyPressed(sf::Keyboard::Key key);
    };
    
  • Pacman.cpp File:

    #include "Pacman.h"
    #include <iostream>
    
    void Pacman::OnKeyPressed(sf::Keyboard::Key key)
    {
    
    }
    
    Pacman::Pacman(float x, float y)
    {
        keyPressedEvent.push_back(*OnKeyPressed);
    }
    

The line that is causing all the trouble is keyPressedEvent.push_back(*OnKeyPressed); this one, even though i'm doing in the same way that i did when testing the "event system" before.

The error is: "Operand of '*' must be a pointer"

I think this error is somehow related to the vector being located in another translation unit, but i don't have any idea on how to solve.

Nícolas
  • 406
  • 3
  • 8
  • 24

1 Answers1

1

The problem is that Pacman::OnKeyPressed is a method (non-static member function), not a regular function. (It has nothing to do with translation units btw). Would that function be static, your code would be OK.

A solution is to use std::function instead of function pointers, and std::bind to make it out of member function (see also Using generic std::function objects with member functions in one class).

numzero
  • 2,009
  • 6
  • 6