Error with dynamic_cast

Error with dynamic_cast

本文关键字:cast dynamic with Error      更新时间:2023-10-16

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

<标题>抽象类
#include "CRoute.h"
class CScreen
{
protected:
    CRoute* m_pRoute;
public:
    virtual ~CScreen();
    virtual void connecToRoute(CRoute* route) = 0;
    virtual void drawRoute() = 0;
};
<标题>派生类
#include "CScreen.h"
class CGUIScreen : public CScreen
{
public:
    void drawRoute();
    void connecToRoute(CRoute* route);
};
<标题>派生类
#include "CScreen.h"
class CCRTScreen : public CScreen
{
   public:
      void drawRoute();
      void connecToRoute(CRoute* route);
};
基类

#include <string>
#include <iostream>
using namespace std;
class CScreen;
class CCRTScreen;
class CGUIScreen;
class CWaypoint
{
    public:
        CWaypoint();
        void print(int format, CScreen* screenType);
};
<标题>派生类
#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;
    }
}

我得到的错误是下一个

    ..myCodeCWaypoint.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)
..myCodeCWaypoint.cpp:184:44: error: expected unqualified-id before ')' token
..myCodeCWaypoint.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)

我读到问题可能是因为我没有转发声明,但我已经这样做了,仍然是一样的问题。

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

一个前向声明,比如你的class CScreen;行,简单地告诉编译器:"有一个叫做'CScreen'的类。稍后我会详细介绍,但现在只要记住这是一个有效的类名,好吗?"

编译器可以用这个类名做一些非常基本的事情;例如,它将接受指针或引用声明。这就是为什么你的print(int format, CScreen* screenType)线工作。你不需要知道关于CScreen的任何信息,只需要知道它的名字就可以声明一个指向它的指针。

但是编译器应该如何接受dynamic_cast与类名?它并不真正了解这个类。特别是,它不知道CGUIScreenCCRTScreen是由CScreen派生的。这就是为什么在使用dynamic_cast时,需要完整的类定义。

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实际上应该是一个抽象基类,并且它可能根本不需要实现文件:

point.h:

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

p。S:恕我直言,我认为你们的班名令人困惑。"Point"绝对是比"Waypoint"更通用的东西,但继承关系恰恰相反。此外,考虑摆脱匈牙利符号。把你的类命名为Screen而不是CScreen等等

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

不是

class CScreen;
class CCRTScreen;
class CGUIScreen;
使用

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