By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

global namespace

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tony Johansson wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

P: n/a
Tony Johansson wrote:
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.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.