Your member function:
void display(int l=this->length)
conceptually equivalent to this:
void display(A * this, int l=this->length);
which means that you use one parameter in the expression, which is the default argument for another parameter that is not allowed in C ++, because Β§8.3.6 / 9 (C ++ 03) says:
By default, arguments evaluate the time that the function calls. The order of evaluation of function arguments is undefined. Therefore, function parameters should not be used in the expression of default arguments , even if they are not evaluated.
Please note that C ++ does not allow this:
int f(int a, int b = a);
The solution is to add one overload that does not accept any parameters:
void display() { display(length);
If you do not want to add another function, select an impossible default value for the parameter. For example, since it describes a length that can never be negative, you can choose -1 as the default value, and you can implement your function as:
void display(int l = -1) { if ( l <= -1 ) l = length;
source share