SFML community forums

Help => General => Topic started by: steli2 on April 04, 2019, 05:31:00 pm

Title: Having trouble with sf::Drawable inheritance
Post by: steli2 on April 04, 2019, 05:31:00 pm
I've been trying to create a particle system that relies on a main class "Particle" that is then inherited by other classes such as "NormalParticle". I want to have the "Particle" class have sf::Drawable and then have the draw() function declared in the child classes, so that I can have a vector of *Particle from which I can use directly window.draw( particle );

ParticleSystem.h
#ifndef PARTICLESYSTEM_H
#define PARTICLESYSTEM_H

#include<queue>

#include<SFML/Graphics.hpp>

namespace GT
{

    class Particle
    {
    public:
        sf::Vector2f position;

        Particle();
    };

    class NormalParticle : public GT::Particle, public sf::Drawable
    {
    public:

       NormalParticle();
    private:
        void draw(sf::RenderTarget& target, sf::RenderStates states) const;
    };

}

#endif // PARTICLESYSTEM_H

 
ParticleSystem.cpp
#include "ParticleSystem.h"

GT::Particle::Particle()
{

}

GT::NormalParticle::NormalParticle()
{

}

 

main.cpp
#include<iostream>
#include<SFML/Graphics.hpp>

#include"ParticleSystem.h"

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 800), "");

    GT::NormalParticle p;

    sf::Clock c;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        window.clear();

        if (c.getElapsedTime().asMilliseconds() >= 16)
        {
            c.restart();
        }

        window.display();
    }

    return 0;
}

 

When I compile the code I get "undefined reference to `vtable for GT::NormalParticle"

I am new to the whole idea of inheritances so I am probably missing something
(also first time posting to the forum)
Title: Re: Having trouble with sf::Drawable inheritance
Post by: FRex on April 04, 2019, 08:57:45 pm
You didn't implement draw at all.
Title: Re: Having trouble with sf::Drawable inheritance
Post by: steli2 on April 04, 2019, 09:16:16 pm
I didn't specify the problem I am so dumb.
So it gives undefined references to the constructors when I have the public sf::Drawable, and it doesn't when it isn't present

Log
||=== Build: Debug in GameEngine (compiler: GNU GCC Compiler) ===|
obj\Debug\src\main.o||In function `ZN2GT14NormalParticleD1Ev':|
src\ParticleSystem.h|19|undefined reference to `vtable for GT::NormalParticle'|
obj\Debug\src\ParticleSystem.o||In function `ZN2GT14NormalParticleC2Ev':|
src\ParticleSystem.cpp|8|undefined reference to `vtable for GT::NormalParticle'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
 
Title: Re: Having trouble with sf::Drawable inheritance
Post by: FRex on April 04, 2019, 10:10:52 pm
Do you have your NormalParticle's draw defined (not just declared) or not?
Title: Re: Having trouble with sf::Drawable inheritance
Post by: steli2 on April 04, 2019, 10:39:10 pm
No I don't have, I've copy pasted all of the code (yeah I know it ain't much so far)
Title: Re: Having trouble with sf::Drawable inheritance
Post by: FRex on April 05, 2019, 02:43:51 am
If a virtual method is not defined (in sf::Drawable it's pure) that might cause an error like you're getting. You should add at least an empty {} for draw in that class and then see if the error is still there.