Search code examples
c++g++crystal-space-3d

g++ complains that "virtual const ... cannot be overloaded"


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?


Solution

  • 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() and InstancePrec() 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:

    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.