Stewart
Stewart

Reputation: 613

Why isn't my virtual function working?

I have an abstract class called camera which PointCamera uses as its super class. For some reason one of the virtual functions throw an error in the debugger and tells me that it is trying to execute 0x00000000. This only happens if the function in question is the last one declared in the abstract class. If I switch the declaration order, then the new last function won't work for the same reason. Why is this happening?

class Camera
{
public:
    //Default constructor
    Camera();

    //Assignment operator
    virtual Camera* clone() = 0;

    //Get a ray
    virtual void KeyCamera() = 0;
    virtual void GetRay(float x, float y, Ray* out) = 0;
};

and

class PointCamera: Camera
{
private:
    //Camera location, target, and direction
    Vector loc, dir, tar, up;
    //Orthonormal vectors
    Vector u, v, w;
    //Virtual plane size
    float plane_width, plane_height;
    int width, height;
    //Distance from the camera point to the virtual plane
    float lens_distance;
    //Pixel size
    float pixelSizex, pixelSizey;

public:
    //Default constructor
    PointCamera();
    //Constructors
    PointCamera(Vector& iloc, Vector& itar);
    PointCamera(Vector& iloc, Vector& itar, Vector& idir);

    //Destructor
    ~PointCamera();

    //Modifiers
    void SetDirection(Vector& idir);
    void SetUp(Vector& iup);
    void SetTarget(Vector& itar);
    void SetLocation(Vector& iloc);
    void SetPlane(int iheight, int iwidth, float iplane_width = -1.0f, float iplane_height = -1.0f);
    void SetLensDistance(float ilens_distance);

    //Implememented method
    virtual void GetRay(float x, float y, Ray* out);
    virtual void SetupRay(Ray* out);

    //Compute orthonormal vectors
    virtual void KeyCamera();
};

Upvotes: 0

Views: 247

Answers (2)

Naveen
Naveen

Reputation: 73503

I just re-compiled everything and it worked

So I assume the abstract base class was declared in one binary (for example, in one dll on windows) and the derived class was in another. In that case, if you don't recompile the binary containing the derived class, it's vtable will not be setup properly and the calls will start behaving weirdly and as @Strager said, you need to have the virtual destructor in the base class.

Upvotes: 0

Thanatos
Thanatos

Reputation: 44344

Ok I just re-compiled everything and it worked. I don't know what went wrong. Thanks for your suggestions.

Check your dependencies. I bet something that should be depending on a header file isn't. When you did a clean build, the source code file that relied on that header file was brought up to date.

Upvotes: 1

Related Questions