473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

global namespace

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
2 7481
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
12
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
3
by: seberino | last post by:
At top of a module I have an integer like so... foo = 4 In a function in that module I know I need to do 'global foo' to get at the value 4. .... IIRC, for dictionaries you DO NOT have...
5
by: george r smith | last post by:
In the MSDN documentation there is a reference to "the global scope". For example "You can declare types directly in the global scope." I have search extensively and can not find a definition of...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
3
by: taps128 | last post by:
I've been reading the namespace specification for the 5.3 relaese, and I can't stop thinking that they have complicated the thing unecessary. Here is what I mean. So far if you call a function...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.