The compiler is complaining about my default parameters?

前端 未结 2 1205
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 11:25

I\'m having trouble with this piece of code , after i took this class from the main.cpp file and splitted it in to .h and .cpp the compiler started complaining about the def

2条回答
  •  半阙折子戏
    2020-12-23 12:08

    You have to specify the default values for the arguments only in the declaration but not in the definition.

     class pBase : public sf::Thread {
         // ....
         void setColor( int _color = -1 );
         // ....
     } ;
    
     void pBase:: setColor( int _color )
     {
         // ....
     }
    

    The default value for an member function's argument can either go in declaration or definition but not both. Quote from ISO/IEC 14882:2003(E) 8.3.6

    6) Except for member functions of class templates, the default arguments in a member function definition that appears outside of the class definition are added to the set of default arguments provided by the member function declaration in the class definition. Default arguments for a member function of a class template shall be specified on the initial declaration of the member function within the class template. [Example:

    class C { 
        void f(int i = 3);
        void g(int i, int j = 99);
    };
    
    void C::f(int i = 3)   // error: default argument already
    { }                    // specified in class scope
    
    void C::g(int i = 88, int j)    // in this translation unit,
    { }                             // C::g can be called with no argument
    

    —end example]

    According to the standard provided example, it should actually work the way you did. Unless you have done like this, you shouldn't actually get the error. I amn't sure why it actually worked in your case with my solution. Probably something visual studio related, I guess.

提交回复
热议问题