簡體   English   中英

為什么我的虛擬功能不起作用?

[英]Why isn't my virtual function working?

我有一個名為camera的抽象類,PointCamera將其用作其超類。 由於某種原因,其中一個虛擬函數在調試器中引發錯誤,並告訴我它正在嘗試執行0x00000000。 僅當所討論的函數是抽象類中聲明的最后一個函數時,才會發生這種情況。 如果我切換聲明順序,則由於相同的原因,新的last函數將無法工作。 為什么會這樣呢?

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

好的,我只是重新編譯了所有內容,並且一切正常。 我不知道出了什么問題。 感謝您的建議。

檢查您的依賴項。 我敢打賭應該不取決於頭文件的東西。 進行干凈構建時,依賴於該頭文件的源代碼文件已更新。

我只是重新編譯了所有內容,但效果很好

因此,我假設抽象基類在一個二進制文件中聲明(例如,在Windows上的一個dll中),而派生類在另一個二進制文件中聲明。 在這種情況下,如果不重新編譯包含派生類的二進制文件,則不會正確設置vtable,並且調用將變得異常,正如@Strager所說,您需要在基類中具有虛擬析構函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM