I was just getting this error: "error: ‘Symbol’ does not name a type"
I found some other StackOverflow questions talking about circular dependencies, but that is not the case here. In fact I could reproduce it by putting this at the top of the source file:
class Symbol{int dummy;};
//class Symbol{int again;};
Symbol global_symbol;
This gives "error: ‘Symbol’ does not name a type" for the 3rd line. If I uncomment the 2nd line, I still get that same error, but just before it I now get: "error: redefinition of ‘class Symbol’" !!
After lots more poking around it appears a 3rd party library has an enum
where Symbol
is defined. Neither that library, nor my own code, uses namespaces, so as moving my code to be inside a namespace was already on my to-do list I'll do that next and hopefully the problem will go away.
But what confuses me is why I didn't get an error on the class Symbol{}
line? If it clashes with an enum, to the extent that I would never be able to instantiate that class, why didn't it complain? I feel like I'm either missing a flag for g++
, or there is a gap in my C++ knowledge. I'm bracing myself for someone to tell me this is a feature not a bug.
(BTW I am using g++ -c -std=gnu++0x -Wall -g -Werror ...
and g++ 4.8.1)
What you are seeing is a form of name hiding: a declaration of an variable or function Symbol
will be found in preference to class Symbol
([basic.scope.hiding] §3.3.10/2). In the cases where C++ allows one declaration to hide another in the same scope, there is always an elaborated-type-specifier which still refers to the hidden declaration. They are so named because only a type (class
or enum
) may be hidden in this way; typedefs and templates cannot. The order of declaration is not significant.
In this case, you can use class Symbol
to refer to the class when the variable or function is in scope:
class Symbol global_symbol;