Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > global namespace

Reply
Thread Tools

global namespace

 
 
Tony Johansson
Guest
Posts: n/a
 
      05-21-2005
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


 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      05-21-2005
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)
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-21-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments