我在嘗試編譯我的代碼時遇到以下錯誤。Can not dynamic_cast
ERROR! ..\myCode\CPOI.cpp:68:41: error: cannot dynamic_cast 'screenType' (of type 'struct CScreen*') to type 'struct CGUIScreen*' (target is not pointer or reference to complete type)
這是我的代碼:
基類:
#include "CRoute.h"
enum screen_t{CRTScreen,GUIScreen};
class CScreen
{
protected:
CRoute* m_pRoute;
public:
virtual ~CScreen();
virtual void connecToRoute(CRoute* route) = 0;
virtual void drawRoute() = 0;
virtual screen_t getScreenType() = 0;
};
派生類:
#include "CScreen.h"
class CGUIScreen : public CScreen
{
public:
void drawRoute();
screen_t getScreenType();
void connecToRoute(CRoute* route);
};
和錯誤:
void CPOI::print(int format, CScreen* screenType)
{
if(dynamic_cast<CGUIScreen*>(screenType))
{
cout << "printing POI GUI " << endl;
}
}
爲什麼發生?
也在派生類中創建虛擬方法,然後重試。您必須重寫所有純虛函數。 – Jepessen
請發佈[最小,完整和可驗證的示例](http://www.stackoverflow.com/help/mcve)。這裏沒有什麼看起來錯你確定你包含了派生類的定義嗎? – Barry