global namespace

Discussion in 'C++' started by Tony Johansson, May 21, 2005.

  1. 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
    Tony Johansson, May 21, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Pete Becker Guest

    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)
    Pete Becker, May 21, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Rolf Magnus Guest

    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.
    Rolf Magnus, May 21, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    552
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,106
  3. Anonymous
    Replies:
    3
    Views:
    509
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    435
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,328
    Howard
    Nov 29, 2004
Loading...

Share This Page