global namespace

T

Tony Johansson

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
 
P

Pete Becker

Tony said:
Question 1: What is global namespace?

I suggest you get a different book. The excerpt you quoted is really
lousy writing, so it's not at all surprising that you're confused by it.
I couldn't follow it, either.
 
R

Rolf Magnus

Tony said:
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.

A C++ book that compares C++ to other programming languages (except maybe C)
is suspicious.
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?

Namespaces have a somewhat similar structure as directories in a file
system. You can define namespaces, sub-namespaces and so on, then you can
put things like classes and functions into them. The global namespace is in
that analogy the same as the top-level directory. Example:

int x;
namespace A
{
int y;
namespace B
{
int z;
}
}

x is in the global namespace, y in namespace A, z in namespace A::B.
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();

This makes no sense. You need to call ::test() from a function. Try:

class Employee
{
public:
Employee()
{
::test();
}
};

class NegativeSalaryExceptions
{ ..... };
}

Where is that definition? ::test() needs to be declared before it is used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top