简体   繁体   English

g++ 抱怨“虚拟常量……不能重载”

[英]g++ complains that “virtual const … cannot be overloaded”

I am attempting to use a third party SDK (Crystal Space) and am encountering some problems.我正在尝试使用第三方 SDK(水晶空间)并且遇到了一些问题。

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) (还有几个类似的块具有相同的 function 名称)

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:在我的 header 文件中,我收到此错误:

error: 'virtual const csEventHandlerID* CSTest::GenericSucc(...) const cannot be overloaded'

This is repeated for GenericPrec, InstaceSucc and InstancePrec.这对 GenericPrec、InstaceSucc 和 InstancePrec 重复。

I haven't been able to find any information on the Googles regarding this error.我无法在 Google 上找到有关此错误的任何信息。 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: tl;博士:

Why can't I overload virtual const functions?为什么我不能重载虚拟 const 函数?

Why can't I overload virtual const functions?为什么我不能重载虚拟 const 函数?
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.重载虚拟 const 函数在语法上是有效的。

All you methods, GenericPrec() , InstaceSucc() and InstancePrec() have the very same prototype.你所有的方法, GenericPrec()InstaceSucc()InstancePrec()都有相同的原型。 To overload a function you must have either:要使 function 过载,您必须具有:

Different no of arguments arguments的不同编号
Different type of arguments不同型号的arguments
Different sequence of arguments arguments的不同序列

The function prototypes you have satisfy neither of the criteria and hence the compiler complains.您不满足任何标准的 function 原型因此编译器抱怨。

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.由于它是第三方 SDK 我认为它必须至少是可编译的,如果是这样,似乎在任何给定时间点都应该启用两个宏中的一个,因此只有一个版本的功能可用。 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.编译器通常会准确地告诉你为什么它不能重载 function。

Check this .检查这个

The compiler clearly gives the message:编译器清楚地给出了信息:

prog.cpp:4: error: ‘virtual void MyClass::doSomething(int) const’ cannot be overloaded
prog.cpp:3: error: with ‘virtual void MyClass::doSomething(int) const’

You have missed out in mentioning the second part of the error message which you posted.您错过了提及您发布的错误消息的第二部分。

The problem is not in virtial or in const , but in two versions of GenericSucc in the same class having exact same signature.问题不在于virtialconst ,而在于具有完全相同签名的同一 class 中的两个版本的GenericSucc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM