精華區beta Programming 關於我們 聯絡資訊
小弟是第一次要開發作virtual destructor,但是一直作不起來 在此請教有經驗的人… 因為我也不知為什麼作不起來,只知道連結時無法成功 所以我下面記錄著我如何來作virtual destructor的方法,code怎麼寫的, 一直到下達編譯跟連結的指令也都有完整記錄下來, 希望有大大們可以直接指正我的錯誤。感激不盡 小弟是在Linux環境下作開發的,然後g++ 版本是 g++ (GCC) 3.3.5 (Debian 1:3.3.5-13) ============================================================= 有一個標準制定,制定了 Spec.h 的東東,而我必須要為Spec.h上的abstract class 來來實作出對應的concrete class。 所以唯獨Spec.h裡的code不能更改就是了。 //file : Spec.h #ifndef _SPEC_H #define _SPEC_H class someException{ public: someException(){} ~someException(){} }; class AbstractBase { public: virtual ~AbstractBase () throw (); virtual void someFunction () throw (someException) = 0; }; #endif //_SPEC_H 我手中有Spec.h的檔案裡面 定了 抽象類別 AbstractBase這個class 因為是要依照標準所制定的API去實現的,不過它有virtual destructor, 所以小弟不知道怎麼怎樣在其它檔案去宣告,才能去實作它。 因為我一直發生在連結錯誤的情形。 因為我必須還要再另外寫兩個檔案 ImplementSpec.h跟 ImplementSpec.cpp 如下所示… //file : ImplementSpec.h #ifndef _IMP_SPEC_H #define _IMP_SPEC_H #include "Spec.h" class ConcreteDerived : public AbstractBase{ public: ConcreteDerived(); virtual ~ConcreteDerived() throw (); virtual void someFunction() throw (someException); }; #endif //_IMP_SPEC_H //file : ImplementSpec.cpp #include "ImplementSpec.h" ConcreteDerived::ConcreteDerived() { //do nothing } ConcreteDerived::~ConcreteDerived() throw () { //do nothing } void ConcreteDerived::someFunction() throw (someException) { //do nothing } 然後我的主程式寫了簡單如下所示的東東。 //file : test.cpp #include "Spec.h" #include "ImplementSpec.h" class ConcreteDerived; int main(){ AbstractBase *someobj = new ConcreteDerived(); delete someobj; return 0; } 小弟是在Linux環境下作開發的,然後g++ 版本是 g++ (GCC) 3.3.5 (Debian 1:3.3.5-13) 接著我就對 test.cpp, ImplementSpec.cpp編成 .o檔,最後再連結生成a.out可執行檔 下面是我紀錄著我在shell環境下執行的指令,然後下面會發生 有關聯結上的錯誤。 usrooo@xxxcluxxxx:~/VirtualDestructor$ g++ -c test.cpp usrooo@xxxcluxxxx:~/VirtualDestructor$ g++ -c ImplementSpec.cpp usrooo@xxxcluxxxx:~/VirtualDestructor$ g++ -o a.out test.o ImplementSpec.o ImplementSpec.o(.text+0x4e): In function `ConcreteDerived::~ConcreteDerived [not-in-charge]()': : undefined reference to `AbstractBase::~AbstractBase [not-in-charge]()' ImplementSpec.o(.text+0x82): In function `ConcreteDerived::~ConcreteDerived [in-charge]()': : undefined reference to `AbstractBase::~AbstractBase [not-in-charge]()' ImplementSpec.o(.text+0xb6): In function `ConcreteDerived::~ConcreteDerived [in-charge deleting]()': : undefined reference to `AbstractBase::~AbstractBase [not-in-charge]()' ImplementSpec.o(.gnu.linkonce.t._ZN12AbstractBaseC2Ev+0x8): In function `AbstractBase::AbstractBase[not-in-charge]()': : undefined reference to `vtable for AbstractBase' ImplementSpec.o(.gnu.linkonce.r._ZTI15ConcreteDerived+0x8): undefined reference to `typeinfo for AbstractBase' collect2: ld returned 1 exit status 因為不知道解決的情況下,只好來這邊請教了。 還望各位大大們的幫助了 謝謝^__________^y -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.224.241.225
drkkimo:就把解構子作成是虛擬的就好了吧218.172.218.185 01/22 01:25
milochen:原本我也是單純這麼想125.224.241.225 01/22 01:30
milochen:可是實作時,卻成功不了><125.224.241.225 01/22 01:30
milochen:於是就來求救了...125.224.241.225 01/22 01:30
drkkimo:下面PO出的方法可行嗎@@?218.172.218.185 01/22 01:49
milochen:大大是指說我的code嗎?125.224.241.225 01/22 01:58
milochen:我的code是先實際寫出來,再post出來的125.224.241.225 01/22 01:58