I have two classes, Messaging and GameObject. Each GameObject object has an instance of Messaging and I need to have a function pointer in Messaging pointing to a function in GameObject.
Furthermore there will also be objects from classes that derive from GameObject that should have the same (have function pointer in their Messaging instance pointing to a function in them).
Below is the code where a GameCharacter object (derived from GameObject) supply the function to be called to its instance of Messaging:
messaging->Register(GameCharacter::DoMessage);
Here is my Messaging class code:
/// Allows an object to send and receive messages.
class Messaging : public Component, public ::Messaging
{
private:
// Forward declaration of GameObject
class GameObject {};
// Define the OnMessageType as a function pointer to a function
// taking Message as parameter and returning void.
typedef void (GameObject:: *OnMessageType)(Message);
// OnMessage function pointer;
OnMessageType OnMessage;
public:
// Assign a function to the function pointer
void Register(OnMessageType func) { OnMessage = func; }
// Forward the Message to the function pointed to by the function pointer
void DoMessage(Message msg) { (this->*OnMessage)(msg); };
};
The GameObject class:
class GameObject
{
public:
/// Unique number used for identification
int id;
/// Reference to the graphical model displayed by this object
int modelReference;
/// GameObject children
std::vector<GameObject> children;
/// Constructor
GameObject();
/// Destructor
~GameObject();
/// Add a child GameObject node
void AddChild(GameObject child);
/// Update Object
virtual void Update();
/// Handles received messages
//virtual void DoMessage(Message msg);
// Provide pointers to all possible components
Components::Transform* transform;
Components::RigidBody* rigidBody;
Components::Messaging* messaging;
// Pointer to message queue
//std::vector<Message> *msgQueue;
private:
/// Send message
void SendMessage(Message msg);
};
The GameObject object would call Register with the function which OnMessage should point to. Then when DoMessage is called the function pointed to in GameObject should be called. So in this case calling DoMessage (in the Messaging instance of GameCharacter) should in turn call DoMessage in the GameCharacter object.
The problem is that I get this error on the DoMessage(Message msg) function:
pointer to member type 'void (Components::Messaging::GameObject::)(Message)' incompatible with object type 'Components::Messaging'
from what I can gather its unhappy about the pointer function types. I have read (on stack overflow posts) that when calling the pointer function one should specify the type to which the member function belong but I can not find how to do that...
Could anybody shed some light on this and explain how I can fix my code?