简体   繁体   中英

namespace: cannot call member function without object

I'm creating a event queue inside a namespace and the goal is be able to call it from anywhere (like a static class function).

So I have the namespace in eventManager.h

namespace atreus {
    class Event;

    class EventManager {
      private:
        std::queue<Event *> events;
      public:
        void pushEvent(Event *event);
        bool pollEvent(Event *event);
    };
}

and then I try to call the pushEvent in another class like: object.cpp

inline void createEvent(sf::Vector2f& n, float penetration, sf::Vector2f velocity, float totalMass)
{
    atreus::Event *event;
    // adding stuff to event 
    atreus::EventManager::pushEvent(event);
}

And then I get this error:

cannot call member function ‘void atreus::EventManager::pushEvent(atreus::Event*)’ without object
 atreus::EventManager::pushEvent(event);

I've tried to add the function createEvent into a class and create a EventManager eventManager inside the namespace but nothing...

void pushEvent(Event *event);

This is a non-static function, it cannot be called without an object. Somewhere you should have an instance of EventManager and use the instance to call pushEvent()

If you want to be able to call this function without object, you need to mark this function as static.

static void pushEvent(Event *event);

You need to create the object before using it.

atreus::EventManager mgr;
mgr.pushEvent(event);

But you need to think of the lifetime of the object as well.

Preferable use an interface to EventManager as class member and inject the class in the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM