Let us reduce your problem:
In C++, an argument takes either the value of ...
* the parameter you pass to it when calling the function
* the default value, if given
If there is no default value for an argument X, and you don't provide a parameter for that X in the function call, your code is invalid.
void foo (int x) {}int main () { foo(); // invalid}
void foo (int x=2) {}int main () { foo(); // valid, as if you passed 2 to foo()}
void foo (int x=2) {}int main () { foo(2); // valid, actually compiles into the same as above}
void foo (int x, int y=2) {}int main () { foo (1); // valid, inside foo() x==1, y==2 foo (5); // valid, inside foo() x==5, y==2 foo (7,8); // valid, inside foo() x==7, y==8 foo (); // invalid, no default value for x}
Then, in C++ you must specify default values from right to left:
void foo (int x, int y=2) {} // validvoid bar (int x=1, int y=2) {} // validvoid frob (int x=2, int y) {} // not valid
Further: For advanced C++ programmers only!
You may specify multiple declarations of a function (but always only one definition), and each declaration may define more default values, from right to left, and only for arguments that don't have a default value yet.
void foo (int x, int y); // okayvoid foo (int x, int y=2); // okayvoid foo (int x=1, int y); // okay, actually, we have foo(int=1,int=2) now!
Imagine it like this:
Applying the first declaration:columns: 0,1 foo ( , ) + foo ( ,=) ----------- = foo ( ,=)Applying the second declaration: foo ( ,=) + foo (=, ) ----------- = foo (=,=)
To have your program well formed, ...
0)
every column above the line must at maximum contain a single "=". If there are two or more, you get "argument xyz already has a default value".
E.g.:
void frob (int x, int y=2); // okayvoid frob (int x, int y=3); // fail, y already has a default value
1)
for every "=" in some column x, one of the
above declarations need a "=" in column x+1, otherwise you get "no standard arguments for the arguments on the right side of xyz have been defined".
E.g.:
void bar (int x, int y); // okayvoid bar (int x=1, int y); // fail, because y does not have a default vale yet // more general: not every argument on the right side of // x has a default value