g++抱怨“virtual const ...不能重载”
我正在尝试使用第三方 SDK(Crystal Space),但遇到了一些问题。
代码(不是我的)如下所示:(
#define CS_EVENTHANDLER_PHASE_LOGIC(x)
CS_EVENTHANDLER_NAMES(x)
CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS
virtual const csHandlerID * GenericPrec
(csRef<iEventHandlerRegistry> &, csRef<iEventNameRegistry> &,
csEventID) const {
return 0;
}
virtual const csHandlerID * GenericSucc
(csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2,
csEventID event) const {
static csHandlerID succConstraint[6];
if (event != csevFrame(r2))
return 0;
succConstraint[0] = FrameSignpost_Logic3D::StaticID(r1);
succConstraint[1] = FrameSignpost_3D2D::StaticID(r1);
succConstraint[2] = FrameSignpost_2DConsole::StaticID(r1);
succConstraint[3] = FrameSignpost_ConsoleDebug::StaticID(r1);
succConstraint[4] = FrameSignpost_DebugFrame::StaticID(r1);
succConstraint[5] = CS_HANDLERLIST_END;
return succConstraint;
}
#define CS_EVENTHANDLER_PHASE_3D(x)
CS_EVENTHANDLER_NAMES(x)
CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS
virtual const csHandlerID * GenericPrec
(csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2,
csEventID event) const {
static csHandlerID precConstraint[2];
if (event != csevFrame(r2))
return 0;
precConstraint[0] = FrameSignpost_Logic3D::StaticID(r1);
precConstraint[1] = CS_HANDLERLIST_END;
return precConstraint;
}
virtual const csHandlerID * GenericSucc
(csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2,
csEventID event) const {
static csHandlerID succConstraint[5];
if (event != csevFrame(r2))
return 0;
succConstraint[0] = FrameSignpost_3D2D::StaticID(r1);
succConstraint[1] = FrameSignpost_2DConsole::StaticID(r1);
succConstraint[2] = FrameSignpost_ConsoleDebug::StaticID(r1);
succConstraint[3] = FrameSignpost_DebugFrame::StaticID(r1);
succConstraint[4] = CS_HANDLERLIST_END;
return succConstraint;
}
还有几个类似的块具有相同的函数名称)
如您所见,它们正在重载虚拟 cosnt 函数。
当我的头文件中有代码时
CS_EVENTHANDLER_PHASE_LOGIC("application.cstest")
,我收到此错误:
error: 'virtual const csEventHandlerID* CSTest::GenericSucc(...) const cannot be overloaded'
对于 GenericPrec、InstaceSucc 和 InstancePrec 重复此操作。
我在 Google 上找不到有关此错误的任何信息。似乎没有任何迹象表明虚拟常量不能重载(开发人员似乎也这么认为),那么编译器的问题是什么?
tl;dr:
为什么我不能重载虚 const 函数?
I am attempting to use a third party SDK (Crystal Space) and am encountering some problems.
The code (not mine) looks like this:
#define CS_EVENTHANDLER_PHASE_LOGIC(x)
CS_EVENTHANDLER_NAMES(x)
CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS
virtual const csHandlerID * GenericPrec
(csRef<iEventHandlerRegistry> &, csRef<iEventNameRegistry> &,
csEventID) const {
return 0;
}
virtual const csHandlerID * GenericSucc
(csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2,
csEventID event) const {
static csHandlerID succConstraint[6];
if (event != csevFrame(r2))
return 0;
succConstraint[0] = FrameSignpost_Logic3D::StaticID(r1);
succConstraint[1] = FrameSignpost_3D2D::StaticID(r1);
succConstraint[2] = FrameSignpost_2DConsole::StaticID(r1);
succConstraint[3] = FrameSignpost_ConsoleDebug::StaticID(r1);
succConstraint[4] = FrameSignpost_DebugFrame::StaticID(r1);
succConstraint[5] = CS_HANDLERLIST_END;
return succConstraint;
}
#define CS_EVENTHANDLER_PHASE_3D(x)
CS_EVENTHANDLER_NAMES(x)
CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS
virtual const csHandlerID * GenericPrec
(csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2,
csEventID event) const {
static csHandlerID precConstraint[2];
if (event != csevFrame(r2))
return 0;
precConstraint[0] = FrameSignpost_Logic3D::StaticID(r1);
precConstraint[1] = CS_HANDLERLIST_END;
return precConstraint;
}
virtual const csHandlerID * GenericSucc
(csRef<iEventHandlerRegistry> &r1, csRef<iEventNameRegistry> &r2,
csEventID event) const {
static csHandlerID succConstraint[5];
if (event != csevFrame(r2))
return 0;
succConstraint[0] = FrameSignpost_3D2D::StaticID(r1);
succConstraint[1] = FrameSignpost_2DConsole::StaticID(r1);
succConstraint[2] = FrameSignpost_ConsoleDebug::StaticID(r1);
succConstraint[3] = FrameSignpost_DebugFrame::StaticID(r1);
succConstraint[4] = CS_HANDLERLIST_END;
return succConstraint;
}
(there are several more blocks like these with the same function names)
As you can see, they are overloading virtual cosnt functions.
When I have the code
CS_EVENTHANDLER_PHASE_LOGIC("application.cstest")
in my header file, I get this error:
error: 'virtual const csEventHandlerID* CSTest::GenericSucc(...) const cannot be overloaded'
This is repeated for GenericPrec, InstaceSucc and InstancePrec.
I haven't been able to find any information on the Googles regarding this error. Nothing seems to indicate that virtual consts can't be overloaded (and the developers seem to think so) so what is the compiler's problem?
tl;dr:
Why can't I overload virtual const functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么我不能重载虚常量函数?
当然可以,前提是您重载它们并且不创建具有相同原型的两个方法。
重载虚拟 const 函数语法上有效。
所有方法,
GenericPrec()
、InstaceSucc()
和InstancePrec()
都具有完全相同的原型。要重载函数,您必须具有:不同的参数数量
不同类型的参数
不同的参数序列
您的函数原型都不满足任何一个标准,因此编译器会抱怨。
由于它是第三方 SDK,我认为它至少必须是可编译的,因此,在任何给定时间点似乎只应启用两个宏之一,因此只有一个版本的功能可用。 这些函数并不是为了重载而编写的。
另外,您错过了给出确切的错误消息。编译器通常会准确地告诉您为什么它不能重载函数。
检查此。
编译器清楚地给出了消息:
您错过了提及您发布的错误消息的第二部分。
Why can't I overload virtual const functions?
Ofcourse you can, provided you overload them and not create two methods with the same prototype.
It is syntactically valid to overload virtual const functions.
All you methods,
GenericPrec()
,InstaceSucc()
andInstancePrec()
have the very same prototype. To overload a function you must have either:Different no of arguments
Different type of arguments
Different sequence of arguments
The function prototypes you have satisfy neither of the criteria and hence the compiler complains.
Since it is a third party SDK I assume it must be at-least compilable and if So It seems only one of the two macros should be enabled at any given point of time, So that only one version of the functions is available. Those functions are not written to be overloaded.
Also, You are missing out on giving an exact error message. The compiler will usually exactly tell you why it cannot overload a function.
Check this.
The compiler clearly gives the message:
You have missed out in mentioning the second part of the error message which you posted.
问题不在
virtial
或const
中,而是在同一类中具有完全相同签名的两个版本的GenericSucc
中。The problem is not in
virtial
or inconst
, but in two versions ofGenericSucc
in the same class having exact same signature.