GCC error: declaration of … shadows a parameter

Problem:

You encounter a GCC error message of the form

shadowing_error.txt
error: declaration of ... shadows a parameter

Somewhere in your code you have a function with an argument

shadow_example.cpp
void doSomething(int arg) {/*...*/}

However, somewhere in the function you want to declare a variable that has the same name as one of the function argument.

Declaring a variable with a name that already refers to another variable is called shadowing. In this case, you shadow a function argument.

For example, in

shadow_example.c
void doSomething(int arg) {
 char* arg = "";
}

char* arg shadows the function argument int arg

Identifying the cause:

The file and the line number refer to the declaration of the variable that shadows the argument.

Resolving the error:

Rename the variable that shadows the argument or rename the argument itself

In the above example, you’d need to rename either char arg (e.g. to char arg2) or int arg.


Check out similar posts by category: Allgemein