Missing default argument - compiler error

前端 未结 5 988
無奈伤痛
無奈伤痛 2021-01-31 15:17
void func ( string word = \"hello\", int b ) {

  // some jobs

}

in another function

 //calling 
 func ( \"\", 10 ) ;

When I have compiled it, compi

5条回答
  •  半阙折子戏
    2021-01-31 16:02

    Parameters with default values have to come at the end of the list because, when calling the function, you can leave arguments off the end, but can't miss them out in the middle.

    Since your arguments have different types, you can get the same effect using an overload:

    void func ( string word, int b ) {
    
      // some jobs
    
    }
    
    void func ( int b ) { func("hello", b); }
    

提交回复
热议问题