我正在嘗試實現類似於解釋here的內容。編譯器要求接口虛擬構造函數?
我作爲一個接口類中定義:
class IInterface
{
public:
virtual bool foo() = 0;
virtual void destroy() = 0;
}
而且和實現類定義爲:
class MyImplementation : public IInterface
{
MyImplementation();
~MyImplementation();
virtual bool foo();
virtual void destroy() {delete this;}
private:
MyImplementation(MyImplementation const&);
void operator=(MyImplementation const&);
}
extern "C" API MyInterface* __cdecl createMyImplementation();
這使用VS2010工作正常發佈模式,但在調試模式下,編譯器爲我這個錯誤:
MyImplementation.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall IInterface::IInterface(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall MyImplementation::MyImplementation(void)" ([email protected]@[email protected])
什麼問題,我該如何解決這個問題?
從我的理解,我們不應該有虛擬構造函數...(見this question)。
編輯:
固定排字錯誤。 foo
確實有一個正文,這是真正的代碼的簡化版本。
的爲什麼破壞功能的說明:
http://www.parashift.com/c++-faq-lite/delete-this.html
http://eli.thegreenplace.net/2011/09/16/exporting-c-classes-from-a-dll
而不是你'destroy'成員,你應該有一個'虛擬'析構函數。 – 5gon12eder 2014-09-25 19:19:55
鏈接器錯誤(如您正在收到的錯誤)對項目的目標文件/庫/ DLL組織非常敏感。到目前爲止,你沒有提供任何細節。更好地描述它。你的聲明如何在源文件中分佈? – AnT 2014-09-25 19:24:31
所以,雖然每個人都對你的模式很糟糕,但我不認爲它會導致你所看到的錯誤....你如何在MyImplementation中包含IInterface? – IdeaHat 2014-09-25 19:25:59