Thread: problem understanding namespace

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    8

    problem understanding namespace

    Hi.

    when I include iostream.h
    cout compiles and getchar() compiles

    when I include iostream
    stating using namespace std
    cout compiles and getchar() compiles

    but without including using namespace std statement
    std::cout compiles and std::getchar() gives error messages???

    does std:: only work for certain functions and how do i know which ones
    thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Location
    Sarajevo, Bosnia and Herzegovina
    Posts
    18
    If you using "using namespace std" just type cout << , cin >> etc.
    If not you may use std::cout etc.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    in your case the problem is that
    getchar is defined in stdio.h

    thus getchar exists on global namespace and NOT in std.
    thus std::getchar would look for "getchar" in namespace std - and of course its not there.

    with using namespace std the comiler looks up getchar in both, global namespace and std, it wont find it in std, but it WILL find it on global namespace. thus it works
    signature under construction

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >getchar is defined in stdio.h
    Which is a deprecated header. The standard header for getchar is cstdio.

    >thus std::getchar would look for "getchar" in namespace std - and of course its not there.
    getchar exists in the global namespace if you use stdio.h, but the std namespace if you use cstdio.

    One issue that you may encounter is with non-conforming compilers (*cough* VC++6 *cough*) and the standard C functions not being properly declared in the std namespace. So this code, while perfectly correct and legal, would fail to compile because the compiler wouldn't find getchar or putchar in the std namespace:
    Code:
    #include <cstdio>
    
    int main()
    {
      int c;
    
      while ( ( c = std::getchar() ) != EOF )
        std::putchar ( c );
    }
    On those compilers, it's actually easier to write code with a using directive rather than try to figure out the quirks and leave off a namespace qualification for those standard names that were inconveniently left out.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    Thanks for the answers.

    explains what i encountered
    will need to do a bit more reading to get to gripswith namespaces.
    because have never heard of the global namspace.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    the global namespace is simply the primary namespace youre in

    if your file looks like:
    Code:
    #include <iostream>
    
    int main() {
      return 0;
    }
    then main is part of the global namespace
    you can think of namespaces like they were directories.
    instead of having all your files in one big directory you move them to many directories (e.g. c:\windows, c:\programs)
    thats basically the same what you do with namespaces
    it avoids conflicts with entities (classes, functions, variables) that have the same name.

    e.g. there is a list class in namespace std.
    now you make your own list and since its a list you want to call it list too.
    so you put it into your own namespace and call it list there.
    so you can access the stl list via std::list
    and your own list via your_namespace::list
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with atoi()?
    By ruthgrin in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2006, 12:25 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. data validation & namespace
    By tiange in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2005, 02:45 AM