Search code examples
c++argumentsdefault

can I pass default argument to parameter of user-defined class type?


I'm getting the error "default argument not at end of parameter list" in the following line.

Date(int m = 1, int d = 2, int y = 1900, Time); // default constructor 

Time is a user-defined class. Is it possible to provide Time with default argument? If yes, how? If no, how can I fix this error?


Solution

  • You can either move Time to the beginning, or give it a default argument based off whatever constructors it has.

    Date(Time, int m = 1, int d = 2, int y = 1900);
    //or
    Date(int m = 1, int d = 2, int y = 1900, Time = Time(/*ctor args can go here*/));
    

    Alternatively if Time has a converting constructor, you could supply a value of that type as the default argument.