Public Group

# [C++] reference subclasses in the parent class

This topic is 3699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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:
[source language="C++"]
virtual void Sprite::CollisionWith(Fire &refe);
virtual void Sprite::CollisionWith(Character &refe);
virtual void Sprite::CollisionWith(Wall &refe);


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! ;)

##### Share on other sites
Using forward declarations:
class Fire;class Wall;class Character;class Sprite{    virtual void CollisionWith(Fire &);    virtual void CollisionWith(Character &);    virtual void CollisionWith(Wall &);};

##### Share on other sites
: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! ;)

##### Share on other sites
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.

##### Share on other sites
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:

#pragma once#include "D3dx9core.h"#include <vector>using namespace std;class Character;class Wall;class Sprite{...

Thanks people! ;)

##### Share on other sites
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...

##### Share on other sites
Those generally mean that you never wrote a function definition for the functions that its complaining about.

##### Share on other sites
Pfff... I am definitely going to leave it for today, I am so tired I keep forgetting the more obvious things.

Everything okay, thanks SiCrane! ;)

• 10
• 18
• 14
• 18
• 15