• Advertisement
Sign in to follow this  

Undefined VTable for Constructor

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya I'm making a 2d game in SDL and I've come to the collision detection. I plan to give every object on the screen (everything is derived from "Object" with variables for the sprite and co-ordinates) a pointer to a "Shape" and then when I want to test for collisions I just test the two shapes. My problem is when I try to make a Shape, here is my Shape class:
class Rect;
class Circle;

class Shape{
protected:
    int m_centerX;
    int m_centerY;
public:
    int &CenX(){return m_centerX;}
    int &CenY(){return m_centerY;}
    void Set(int x, int y){CenX() = x; CenY() = y;}

    virtual void SetTL(int x, int y){CenX() = x; CenY() = y;}

    virtual bool CheckOverlap(Shape *other);
    virtual bool CheckOverlap(Rect *other);
    virtual bool CheckOverlap(Circle *other);
};

class Rect : public Shape{
protected:
    int m_extendX;
    int m_extendY;
public:
    int &ExtendX(){return m_extendX;}
    int &ExtendY(){return m_extendY;}

    int MinX(){return CenX() - ExtendX();}
    int MaxX(){return CenX() + ExtendX();}
    int MinY(){return CenY() - ExtendY();}
    int MaxY(){return CenY() + ExtendY();}

    int GetWidth(){return 2*ExtendX();}
    int GetHeight(){return 2*ExtendY();}
    void SetWidth(int width){ExtendX() = width/2;}
    void SetHeight(int height){ExtendY() = height/2;}
    void SetTL(int x, int y){
        CenX() = x + ExtendX();
        CenY() = y + ExtendY();
    }

    bool CheckOverlap(Shape *other);
    bool CheckOverlap(Rect *other);
    bool CheckOverlap(Circle *other);

    Rect(int x, int y, int width, int height){
        CenX() = x + width/2;
        CenY() = y + width/2;
        SetWidth(width);
        SetHeight(height);
    }
};

class Circle : public Shape{
protected:
    int m_radius;
public:
    int &Radius(){return m_radius;}

    void SetTL(int x, int y){
        CenX() = x + Radius();
        CenY() = y + Radius();
    }

    bool CheckOverlap(Shape *other);
    bool CheckOverlap(Rect *other);
    bool CheckOverlap(Circle *other);

    Circle(int x, int y, int radius){
        Set(x, y);
        Radius() = radius;
    }
};

And if you needed it my problem code is:
Shape* myShape = new Circle(50, 50, 20);

And my error message:
Switching to target: default
Compiling: main.cpp
Linking console executable: SDLapp.exe
.objs\main.o:main.cpp:(.text$_ZN5ShapeC2Ev[Shape::Shape()]+0x8): undefined reference to `vtable for Shape'
collect2: ld returned 1 exit status

Any help would be much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Peter Conn
Any help would be much appreciated.


GCC emits the vtable into the compilation unit containing the destructor. Try giving your Shape class a virtual destructor (which you need to do anyway, since you'll be deleting a pointer-to-base-class of your derived obects).

Share this post


Link to post
Share on other sites
Usually, GCC emits the "Undefined reference to 'vtable for <classname>'" error when you define a class with some virtual member functions and forget to define one of those virtual functions. Double-check that you've defined all the functions that you declare.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement