[source language="C++"]
virtual void Sprite::CollisionWith(Fire &refe);
virtual void Sprite::CollisionWith(Character &refe);
virtual void Sprite::CollisionWith(Wall &refe);
[C++] reference subclasses in the parent class
Hi everybody!
I was trying to implement some kind of double dispatch for collision detection of my game. I read this article and it explained it perfectly. But I have a silly problem. I don't know if it's because I've been the whole day working in this and I'm tired, but I think it should be simple.
I have a class named Sprite, and several derived from it (Fire, Character, Wall ...). So, in order to implement this double dispatch thing, I should have a virtual method in the parent class (Sprite class) for each one of its subclasses and they should be something like this:
Well, the problem is that I can't make this declarations in Sprite class because it wouldn't recognize Fire, Character, Wall and any of the subclasses. I tried to include "Fire.h" in its parent class (Sprite) but I came with a lot of errors, anyway I think this is not supposed to be done because Fire.h (and all the other subclasses) contain another include to Sprite.h (its parent class).
So what is the correct design for this? I hope somebody can help me.
Thank you in advance! ;)
Using forward declarations:
class Fire;class Wall;class Character;class Sprite{ virtual void CollisionWith(Fire &); virtual void CollisionWith(Character &); virtual void CollisionWith(Wall &);};
:D I knew it would be easy. And this is a usual thing, right? It's not like it's a bad design or anything, right?
Thanks a lot rip-off! ;)
Thanks a lot rip-off! ;)
you must include the header files before the definition of functions that use said forward declared classes.
Usually I put the forward declarations in the headers and all the includes in the .cpp file.
Usually I put the forward declarations in the headers and all the includes in the .cpp file.
Sorry, but by doing this I am getting a lot of linking errors... Am I doing anything wrong?
My Sprite.h file starts like this:
Thanks people! ;)
My Sprite.h file starts like this:
#pragma once#include "D3dx9core.h"#include <vector>using namespace std;class Character;class Wall;class Sprite{...
Thanks people! ;)
Sorry, they are all unresolved external symbol errors (I have never understood this kind of error).
Sprite.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Sprite::CollisionWith(class Wall &)" (?CollisionWith@Sprite@@UAEXAAVWall@@@Z)
Sprite.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Sprite::CollisionWith(class Character &)" (?CollisionWith@Sprite@@UAEXAAVCharacter@@@Z)
Sprite.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Sprite::CollisionWith(class Sprite &)" (?CollisionWith@Sprite@@UAEXAAV1@@Z)
Wall.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Wall::CollisionWith(class Wall &)" (?CollisionWith@Wall@@UAEXAAV1@@Z)
Wall.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Wall::CollisionWith(class Character &)" (?CollisionWith@Wall@@UAEXAAVCharacter@@@Z)
Hope it helps...
Sprite.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Sprite::CollisionWith(class Wall &)" (?CollisionWith@Sprite@@UAEXAAVWall@@@Z)
Sprite.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Sprite::CollisionWith(class Character &)" (?CollisionWith@Sprite@@UAEXAAVCharacter@@@Z)
Sprite.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Sprite::CollisionWith(class Sprite &)" (?CollisionWith@Sprite@@UAEXAAV1@@Z)
Wall.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Wall::CollisionWith(class Wall &)" (?CollisionWith@Wall@@UAEXAAV1@@Z)
Wall.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Wall::CollisionWith(class Character &)" (?CollisionWith@Wall@@UAEXAAVCharacter@@@Z)
Hope it helps...
Those generally mean that you never wrote a function definition for the functions that its complaining about.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement