2010-08-13 49 views
4

我正在告訴我錯誤 - 「拋出不同的異常」,在C++

error: declaration of 'virtual FXHost::~FXHost()' throws different exceptions 
error: than previous declaration 'virtual FXHost::~FXHost() throw()' 

我不知道如何開始解決這個錯誤,我以前從來沒有遇到過這種。

在我的.h

我:

public: 
    virtual       ~FXHost() throw(); 

在我的.cpp我:

FXHost::~FXHost() 
{ 
    gHost = NULL; 
} 

指針讚賞。

+2

不要從析構函數中拋出異常! http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor – nos 2010-08-13 00:31:06

+2

你的頭聲明函數不拋出異常,但你的定義不 – 2010-08-13 00:31:50

+0

@nos我認爲這就是他正在嘗試用他的投擲() – 2010-08-13 00:33:25

回答

5

函數聲明結尾的throw()是一個異常規範。這意味着函數永遠不會拋出異常。這不能在派生類中被覆蓋(僅限於進一步限制),因此是錯誤。

由於您的實現本身不會拋出異常,所有您需要的是將throw()添加到您的析構函數聲明中。

See here why you should (not) use this (mis)feature of C++

2

你想:

FXHost::~FXHost() throw() 
{ 
    gHost = NULL; 
} 

雖然這個析構函數提出了一種糟糕的設計 - 這是不可能的析構函數將通過設置一個指針,即使是一個全球性的指針,爲NULL正常工作簡單。

+0

看起來像重置靜態,以便例如一個老人被毀後可以更換一個單身人士。沒有看到這個問題。所有真正的清理可能在實施RAII的成員主題中。 – 2010-08-13 00:48:58

+0

@Ben那麼誰在摧毀舊的?和「科目」? – 2010-08-13 00:51:18

+0

我打算輸入「子對象」。 – 2010-08-13 02:46:29

3
FXHost::~FXHost() throw() 
{ 
    gHost = NULL; 
} 

您的實現必須至少在異常拋出其聲明的條款限制。