Namespaces are missing or colliding

Problem: GNU Compiler Collection (GCC) 4.6.3 supports namespaces. A namespace holds unique identifiers. You can define an identifier in multiple namespaces; you can also have a different definition in each namespace. To access a specific identifier in a namespace, you must specify that namespace. If you specify an identifier without a namespace, you can get error messages about an expected identifier. For example:
  • test09.cpp:2: error: expected identifier before numeric constant
  • test09.cpp:2: error: expected ‘;’ before numeric constant
  • test09.cpp:2: error: expected unqualified-id before numeric constant
  • test09.cpp:8: error: expected identifier before numeric constant
  • test09.cpp:8: error: expected unqualified-id before numeric constant
You might also get error messages because of collisions between namespaces. For example:
  • Test10.cpp:8: error: call of overloaded function is ambiguous, where function is the name of the overloaded function.
Solution: If you use multiple namespaces or if you encounter namespace issues such as missing namespaces or namespace collisions, add a global scope qualifier (the namespace name followed by two colons (namespace::) before each of the affected identifiers. Alternatively, you can add this statement to your program:
  • using namespace namespace
For example, the std namespace contains the standard identifiers. There are two ways to access the identifiers in this standard namespace:
  • Add std:: before each identifier in your program; for example, std::cout
  • Add the following statement to your program: using namespace std;
    Note: Adding the using statement means that the std namespace is used for all identifiers that are not explicitly prefixed with a namespace, unless the identifier also is included in another declared namespace, which causes a compiler ambiguity. If you have identifiers that are defined in the std namespace and also in another namespace where the other namespace is not specified, the identifier in the std namespace is used.