SDL - Trouble with derivated classes

Hi at all! I'm making some kind of game just to learn the SDL library.
I made two vectors where I respectively store pointers and some constants that I need. All was going fine until I made a function to check collision boxes.
I made a class named "Instance" which represents all the game components such as players, blocks, enemies, etc... and derivated classes like the Player, which is derivated from Instance. So, in the function collide_rect (the name collide_rect because of SDL_Rect) I pass two pointers which, in this case, are the two instances of the class Player, which are derivated from Instance. So, since I want to "generalize" the function, I declare local pointers of the function with the "Instance" type, and not "Player" type. When I compile, errors are vague for me because I don't have much experience. I'm just learning from myself.

I uploaded the project here:
http://github.com/ReDevilGames/test

Also there is a PNG which contains the game sprites but I think it's not important...

Anyway, can some of you tell me what is the error?

Thanks in advance!
Last edited on
"The repository is empty."
Anyways, from what I know, you're probably trying to access Player-only stuff when working with Instance types.
This is not really possible, you should request Players in the first place and leave Instances aside.

EDIT:
You can also post (hopefully short) code within code tags ( [ code][ /code] ).
Errors should be (hopefully short but descriptive: we don't need your file paths and C#### error code,
but the description and the line of where it happens) within output ( [ output][ /output] ) tags.

RE-EDIT:
Just found out it's actually here:
https://github.com/ReDevilGames/test
In the hope that this is the actual project, i'm giving it an eye.

RE-EDIT:
The collide_rect on its own doesn't seem to be the issue.
The problem is probably around Player.h.
Are you using simple C++ or C++11?
On the regular C++ you cannot do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Player : public Instance
{
    public:
        SDL_Texture *curTexture = nullptr;
        SDL_Rect playerRect, playerPos;
        int player;
        int dirh = 1; // <- no
        float x = 0; // <- no
        float hspeed = 0; // <- no
        float animx = 0; // <- no
        Player(SDL_Renderer *renderTarget, int player);
        ~Player();
        void stepEvent(const Uint8 *keyState);
        void endStepEvent();
        void drawEvent(SDL_Renderer *renderTarget);
};

Instead, you must put them in the constructor.
Last edited on
I'm using CodeBlocks with the GCC Compiler and the C++11 argument setted.
The errors are:
\obj\Debug\src\instance.o - In function `ZN8InstanceC2Ev':
instance.cpp |3| undefined reference to `vtable for Instance'
obj\Debug\src\instance.o - In function `ZN8InstanceD2Ev':
instance.cpp |5| undefined reference to `vtable for Instance'|
Build finished: 2 errors, 0 warnings (0 minutes, 5 seconds)

I really don't know what is this "vtable". I also tried searching around the Internet but nothing worked...
Please, can you help me?
Thanks in advance!
Last edited on
Just found out in Instance.h you declare three virtual functions you don't define.
Virtual functions are stored into vtables, so undefined reference to vtable means a virtual function has no body.
You can EITHER add an empty body, OR you can make them pure virtual functions as follows:
virtual void function() =0;
Note: if you make them virtual, ALL derived classes must override that function, otherwise you'll get "Abstract Class"-like errors.
I thought I had to override these three functions since I have derivated classes. Ok, I have understood the mistake. Thanks to you all!!
Last edited on
You can, but in your way, you are able to NOT redefine one, and the default body (Instance::function) will be called.
Again, to avoid having a default body, you have to put =0, but this forces ALL derived classes to redefine the function, or it won't compile.
Registered users can post here. Sign in or register to post.