dynamic_cast 错误

Error with dynamic_cast

我的 dynamic_cast 代码有问题。我花了很多时间试图为此找到解决方案,但我仍然没有找到答案。我读到问题可能是因为我没有写前向声明,但我已经这样做了,但仍然有同样的问题。

摘要class

#include "CRoute.h"

class CScreen
{
protected:

    CRoute* m_pRoute;

public:
    virtual ~CScreen();
    virtual void connecToRoute(CRoute* route) = 0;
    virtual void drawRoute() = 0;
};

派生class

#include "CScreen.h"

class CGUIScreen : public CScreen
{

public:
    void drawRoute();
    void connecToRoute(CRoute* route);

};

派生class

#include "CScreen.h"
class CCRTScreen : public CScreen
{
   public:
      void drawRoute();
      void connecToRoute(CRoute* route);

};

基地Class

#include <string>
#include <iostream>

using namespace std;

class CScreen;
class CCRTScreen;
class CGUIScreen;

class CWaypoint
{
    public:
        CWaypoint();
        void print(int format, CScreen* screenType);

};

派生class

#include <iostream>
#include <string>
#include "CWaypoint.h"

using namespace std;

class CScreen;
class CCRTScreen;
class CGUIScreen;

class CPOI : public CWaypoint
{
    public:
        void print(int format, CScreen* screenType);

};

CPOI的方法

void CPOI::print(int format, CScreen* screenType)
{

    if(dynamic_cast<CGUIScreen*>(screenType)) ---> Here is the error <<----
    {
        cout << "printing POI GUI " << endl;
    }

    else if(dynamic_cast<CCRTScreen*>(screenType)) ---> Here is the error <<----
    {
        cout << "printing POI CRT " << endl;
    }
}

我遇到的错误是下一个

    ..\myCode\CWaypoint.cpp:184:41: error: cannot dynamic_cast 'screenType' (of type 'struct CScreen*') to type 'struct CGUIScreen*' (target is not pointer or reference to complete type)
..\myCode\CWaypoint.cpp:184:44: error: expected unqualified-id before ')' token
..\myCode\CWaypoint.cpp:188:46: error: cannot dynamic_cast 'screenType' (of type 'struct CScreen*') to type 'struct CCRTScreen*' (target is not pointer or reference to complete type)

错误消息告诉你的是它不知道 CScreen 或任何派生的定义是什么 类 因为你已经向前声明了它们但没有包含它们的定义。

而不是

class CScreen;
class CCRTScreen;
class CGUIScreen;

使用

#include "CCRTScreen.h"
#include "CGUIScreen.h"

I read that the problem could be because I didn't write forward declarations but I have already done that and still with the same problem.

恰恰相反;您的前向声明是导致错误的原因。

前向声明,例如您的 class CScreen; 行,只是告诉编译器:"There is a class called 'CScreen'. I'll give you more details later, but for now just keep in mind that this is a valid class name, OK?"

然后编译器可以使用那个 class 名称做非常基本的事情;例如,它将接受指针或引用声明。这就是为什么您的 print(int format, CScreen* screenType) 行有效。除了名称之外,您无需了解 CScreen 的任何信息即可声明指向它的指针。

但是编译器应该如何接受名称为 class 的 dynamic_cast?它实际上对 class 一无所知。特别是,它不知道 CGUIScreenCCRTScreen 是从 CScreen 派生的。这就是为什么在您使用 dynamic_cast 时,需要完整的 class 定义。

CWaypointCPOI 的头文件(可能称为 waypoint.hpoint.h?),因此可以安全地使用前向声明。正如您所做的那样:

waypoint.h:

class CScreen;
class CCRTScreen;
class CGUIScreen;

class CWaypoint
{
    public:
        CWaypoint();
        void print(int format, CScreen* screenType);

};

point.h:

class CScreen;
class CCRTScreen; // not necessary but not invalid
class CGUIScreen; // not necessary but not invalid

class CPOI : public CWaypoint
{
    public:
        void print(int format, CScreen* screenType);

};

然而,实现文件(可能称为 waypoint.cpppoint.cpp?),在您使用 dynamic_cast:

时需要完整的定义

point.cpp:

#include "point.h"
#include "screen.h"
#include "gui_screen.h"
#include "crt_screen.h"

#include <iostream>

using std::cout;
using std::endl;

void CPOI::print(int format, CScreen* screenType)
{

    if(dynamic_cast<CGUIScreen*>(screenType))
    {
        cout << "printing POI GUI " << endl;
    }

    else if(dynamic_cast<CCRTScreen*>(screenType))
    {
        cout << "printing POI CRT " << endl;
    }
}

顺便说一下,CWaypoint 似乎实际上应该是一个抽象基础 class,而且它可能根本不需要实现文件:

point.h:

class CScreen;

class CWaypoint
{
    public:
        virtual ~CWaypoint() {}
        virtual void print(int format, CScreen* screenType) = 0;

};

P.S:如果我可以这么说,我认为您的 class 名称令人困惑。 "Point" 肯定比 "Waypoint" 更通用,但继承关系正好相反。另外,考虑摆脱匈牙利符号。只需调用您的 classes Screen 而不是 CScreen