2009-11-27 78 views
0

我有一個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(); 
}; 
+0

重現錯誤的最小代碼段會很好。看起來你在嘗試調用函數時做了一些奇怪的事情,或者你設法使vtable無法正確初始化。 – Donnie 2009-11-27 04:17:13

+0

你在調用構造函數中的虛函數嗎?如果是這樣,那可能是你的問題。 – strager 2009-11-27 04:18:20

+0

好吧,我只是重新編譯一切,它的工作。我不知道哪裏出了問題。感謝您的建議。 – Stewart 2009-11-27 04:26:26

回答

1

好吧,我只是重新編譯的一切,它的工作。我不知道哪裏出了問題。感謝您的建議。

檢查您的依賴關係。我敢打賭,應該取決於頭文件的東西不是。當你做了一個乾淨的構建時,依賴於該頭文件的源代碼文件被更新了。

0

我只是重新編譯的一切,這 工作

所以我認爲抽象基類是在一個二進制文件(例如,在Windows上一個DLL)和派生類在聲明另一個。在這種情況下,如果你不重新編譯包含派生類的二進制文件,它的vtable將無法正確設置,並且調用將開始異常行爲,正如@Strager所說的,你需要在基類中擁有虛擬析構函數。

相關問題