Hello!
I'm reading a book about C++ and there is something that I don't understand
so I ask you.
Below I have the text from the book and the code from the file where main is
located and some namespace definition with class definitions.
The book says
"C++ has a global anonymous namespace that is similar to Java's global
anonymous package. All declarations not explicitly placed in named
namespaces are placed in the global namespace. A single namespace define a
scope, so all names that occur in this namespace must be distinct. Anything
that can be globally defined can also be defined in a namespace, including
constant, variables(with initializations if desired). stand-alone functions,
classes and nested namespaces.
Outside the namespace, you use the scope operator :: to refer to member of
this namespace; for example
Company::Employee e;
Inside the namespace, in definitions of its members, you can refer to a name
Id in the global namespace, using ::Id, For example, if the namespace
Company has a function test(), to refer to a global test, you use ::test()"
Question 1: What is global namespace?
Question 2: I have this main file below and a global function called test.
In the Employee class I have a call to this global function test in this way
::test(); I thought I refering to the global namespace here but probably I
don't because of the compile errors.
This doesn't work I get the following compile error.
Compiling...
start.cpp
c:\documents and settings\tony\com\slask\singleton.h(9) : error C2039:
'test' : is not a member of '`global namespace''
c:\documents and settings\tony\com\slask\singleton.h(9) : error C2065:
'test' : undeclared identifier
C:\Documents and Settings\Tony\COM\slask\start.cpp(7) : error C2373: 'test'
: redefinition; different type modifiers
Error executing cl.exe.
slask.exe - 3 error(s), 0 warning(s)
Here is the main file
***************
#include <iostream>
#include "singleton.h"
using namespace std;
using namespace Company;
void test()
{}
int main()
{
Employee temp;
return 0;
}
Here we have a namespace definition with two class definitions.
***********************************************
namespace Company
{
class Employee
{
public:
::test();
};
class NegativeSalaryExceptions
{ ..... };
}
//Tony