Error with dynamic_cast
Error with dynamic_cast
我的代码与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
与类名?它并不真正了解这个类。特别是,它不知道CGUIScreen
或CCRTScreen
是由CScreen
派生的。这就是为什么在使用dynamic_cast
时,需要完整的类定义。
CWaypoint
和CPOI
(可能被称为waypoint.h
和point.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.cpp
和point.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"
- 如何理解C++标准N3337中的expr.const.cast子句8
- C++Cast运算符过载
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- C++类中的二维"dynamic"数组?
- 错误:"cast"未命名类型void setCastDescription(std::string
- 通过使用 const-cast 的非常量引用来延长临时的寿命
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- protobuf in C++ with dynamic binding for google::protobuf::M
- 警告的原因是什么:"when type is in parentheses, array cannot have dynamic size"?
- C++:"Expected '(' for function-style cast or type construction"错误
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- CUDA 错误:"dynamic initialization is not supported for __device__, __constant__ and __shared__ variabl
- Gtk+ g_signal_connect() 和 C++ lambda 会导致"invalid cast"错误
- 如何修复'The procedure entry point SDL_RWclose could not be located in the dynamic link library'
- Shared_ptr cast vs static_cast speed
- 在 iOS 上使用 Aruco 构建 OpenCV 时"Functional-style cast from id to double is not allowed"
- "The ordinal 344 could not be located in the dynamic link library"
- 覆盖 CAST 运算符(我认为它被称为向下转换)
- Dynamic Cast C++ Fail
- dynamic-cast-c++dynamic_cast错误处理