0

I have a function declaration

int checkWinsockError(int errorCode, const char *errorType, bool getError, int WSAAPI cleanupFunc(), struct addrinfo *addrToFree, SOCKET *socketToClose, bool sockError);

and a function definition.

int checkWinsockError(int errorCode, const char *errorType, bool getError=false, int WSAAPI cleanupFunc()=nullptr, struct addrinfo *addrToFree=nullptr, SOCKET *socketToClose=nullptr, bool sockError=false) {
      // error handling code
}

When I make a call to this function,

checkWinsockError(10000, "error");

I get the error ''checkWinsockError': function does not take 2 arguments'. This call to the function

checkWinsockError(10000, "error", false);

overriding the first default argument gives the error ''checkWinsockError': function does not take 3 arguments'. This continues until I override the last default argument, sockError, with a function call like this.

checkWinsockError(10000, "error", false, nullptr, nullptr, nullptr, false);

I only began experiencing this error after adding sockError to the list of parameters. Why does the compiler force me to pass a value for this parameter when there is a default argument? I am using Visual Studio 2017 and the Winsock library.

TwistyTurtleFish
  • 61
  • 1
  • 2
  • 8
  • 2
    Does this answer your question? [Where to put default parameter value in C++?](https://stackoverflow.com/questions/4989483/where-to-put-default-parameter-value-in-c) TL;DR put default values in the declaration, not the definition. – Lukas-T Feb 09 '20 at 18:13
  • @churill Yeah that fixes it, thanks for the help – TwistyTurtleFish Feb 09 '20 at 18:16

1 Answers1

0

In the point of the function call the compiler sees only the declaration of the function that does not have default arguments.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335