Sign in to follow this  
starstriker1

Issues with Acyclical Visitor

Recommended Posts

starstriker1    245
I'm attempting to use the Acyclical Visitor for passing messaged between game objects. In this case, the game objects themselves would be the visitors while the messages would be visited. The code looks something like this:
class GameObject; //Forward declaration
class MessageBase
{
public:
	MessageBase()
	{}
	virtual ~MessageBase()
	{}

	//Check to see whether we can send to this object or not. Use a
	//dynamic_cast to see whether it casts to the desired vistor type or
	//not, and call the relevant visitor if so!
	virtual void send(GameObject *obj) const = 0;
};

class CollisionMessage; 
class CollisionMessageReciever //A game object would inherit from this
{ 
private: 
	virtual void recieve(const CollisionMessage *msg) = 0; 
	friend class CollisionMessage; 
}; 
class CollisionMessage : public MessageBase 
{ 
public: 
	virtual void send(GameObject *obj) const 
	{ 
		CollisionMessageReciever *rcvr = dynamic_cast<CollisionMessageReciever*>(obj); 
		if(rcvr) 
			rcvr->recieve(this); 
	} 
	CollisionMessageData data; 
}; 


However, I get the following error: error C2681: 'GameObject *' : invalid expression type for dynamic_cast I've done a little digging, and it may have something to do with C++ checking whether GameObject is in CollisionMessageReciever's inheritance graph at compile time. However, I haven't been able to figure out a way around this problem. I have an object defined elsewhere that inherits from both GameObject and CollisionMessageReciever, but this doesn't appear to have an effect. It's also worth noting that at this point, GameObject has only been forward declared. However, including "GameObject.h" (with the full definition) does not resolve the compile error. Anyone have any ideas?

Share this post


Link to post
Share on other sites
SiCrane    11839
Does your GameObject have virtual functions? Does your GameObject.h files directly or indirectly include the header that your message classes are defined in?

Share this post


Link to post
Share on other sites
starstriker1    245
At the time of writing, only the forward declaration for GameObject was being provided. I've since fixed it, and it's working now. Stupid mistake on my part, apologies.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this