For anyone with this situation: I saw this error when I accidentally used my_first_scope::my_second_scope::true
in place of simply true
, like this:
bool my_var = my_first_scope::my_second_scope::true;
instead of:
bool my_var = true;
This is because I had a macro which caused MY_MACRO(true)
to expand into my_first_scope::my_second_scope::true
, by mistake, and I was actually calling bool my_var = MY_MACRO(true);
.
Here's a quick demo of this type of scoping error:
Program (you can run it online here: https://onlinegdb.com/BkhFBoqUw):
#include <iostream>
#include <cstdio>
namespace my_first_scope
{
namespace my_second_scope
{
} // namespace my_second_scope
} // namespace my_first_scope
int main()
{
printf("Hello World\n");
bool my_var = my_first_scope::my_second_scope::true;
std::cout << my_var << std::endl;
return 0;
}
Output (build error):
main.cpp: In function ‘int main()’: main.cpp:27:52: error: expected unqualified-id before ‘true’ bool my_var = my_first_scope::my_second_scope::true; ^~~~
Notice the error: error: expected unqualified-id before ‘true’
, and where the arrow under the error is pointing. Apparently the "unqualified-id" in my case is the double colon (::
) scope operator I have just before true
.
When I add in the macro and use it (run this new code here: https://onlinegdb.com/H1eevs58D):
#define MY_MACRO(input) my_first_scope::my_second_scope::input
...
bool my_var = MY_MACRO(true);
I get this new error instead:
main.cpp: In function ‘int main()’: main.cpp:29:28: error: expected unqualified-id before ‘true’ bool my_var = MY_MACRO(true); ^ main.cpp:16:58: note: in definition of macro ‘MY_MACRO’ #define MY_MACRO(input) my_first_scope::my_second_scope::input ^~~~~