为什么我的虚拟功能不起作用?

时间:2009-11-27 04:15:30

标签: c++

我有一个名为camera的抽象类,PointCamera将其用作超类。由于某种原因,其中一个虚函数在调试器中抛出一个错误并告诉我它正在尝试执行0x00000000。只有当有问题的函数是抽象类中声明的最后一个函数时,才会发生这种情况。如果我切换声明顺序,那么新的最后一个函数将不会出于同样的原因。为什么会这样?

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;
};

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();
};

2 个答案:

答案 0 :(得分:1)

  

好的,我只是重新编译了所有内容并且有效。我不知道出了什么问题。谢谢你的建议。

检查您的依赖项。我打赌应该依赖于头文件的东西不是。当你进行干净的构建时,依赖于该头文件的源代码文件是最新的。

答案 1 :(得分:0)

  

我只是重新编译了所有内容   工作

所以我假设抽象基类是在一个二进制文件中声明的(例如,在Windows上的一个dll中),派生类在另一个中。在这种情况下,如果你不重新编译包含派生类的二进制文件,它的vtable将无法正确设置,并且调用将开始表现奇怪,并且正如@Strager所说,你需要在基类中使用虚拟析构函数。 / p>

相关问题