Thread: namespace std

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    namespace std

    Why do people use namespace std for iostream functions if it's included in the header?
    Last edited by blankstare77; 07-23-2005 at 04:30 PM.

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    That sentance made no sense what so ever.

    The namespace std includes most of your standard C++ I/O functions, and there 'using namespace std;' is typed early on in a program. This is of course down to personal taste - some prefer to use std::cout etc to show cout is a member of the std namespace.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A namespace is a way of grouping names together so if two different things in a program have the same name the compiler will be able to tell them apart. All of the names in the standard library are part of the std namespace. So if you want to use cout from <iostream>, you have to tell the compiler that you want std::cout. Another way to do this that some people use is by putting a using directive in their code to tell the compiler to look in a particular namespace for the name they use. So if you type using namespace std; and then later use cout, the compiler will check in namespace std for something called cout and will be able to find it.

    Pre-standard headers (like <iostream.h>) don't have the namespace protection, which is why you can find old code without the std parts. However, the benefits of using the new standard library far outweigh the cost of typing a few extra characters, so everybody should be using the new headers (<iostream>) with the std namespace.

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Here's an example of using the namespace:


    Code:
    //Division Program
    //An example of using namespace
    #include <iostream>
    
    using namespace std; //if you include this, you DONT have to put std:: before cout,cin, and endl
    
    int main()
    {    
        int num1, num2, quotient;
        cout << "Enter your first whole number:";
        cin >> num1;
        cout << "Enter your second whole number:";
        cin >> num2;
        quotient=num1/num2;
        cout << "Your quotient is " << quotient << endl;
        cin.ignore();
        cin.get();
        return 0;
    }
    The same example without using the namespace:

    Code:
    //Division Program
    
    #include <iostream>
    
    //Since there is no namespace, you must put std:: before cout & cin, and endl
    
    int main()
    {    
        int num1, num2, quotient;
        std::cout << "Enter your first whole number:";
        std::cin >> num1;
        std::cout << "Enter your second whole number:";
        std::cin >> num2;
        quotient=num1/num2;
        std::cout << "Your quotient is " << quotient << std::endl;
        std::cin.ignore();
        std::cin.get();
        return 0;
    }
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Uhh here's sample code that works fine in Visual C++ 6.

    Code:
    <iostream.h>
    
    void main() {
           cout << "Hi this is just a dummy program" << endl;
    }

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    #include <iostream.h> -> Wrong! iostream.h is a deprecated header and is no longer standard.

    void main() -> Very wrong! void main has never ever been standard, main must always return an int!
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    No longer "standard"? I use it and it's fine. I see it all the time on websites.

    I used void because in those parameters it doesn't return a value. It just prints things on the screen (cout).

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You use it now but when you move to a newer compiler that is more standard compliant all your code is wrong and you need to rewrite it because you are too lazy to follow the current standard. And those websites that uses those are old, outdated and in general not good learning resources.

    And no no no no no no no NO NOOOOO!!!!!!!!! main always alwayas ALWAYS returns an int!!!!!
    Last edited by Shakti; 07-23-2005 at 04:48 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Uhh, on my compiler it produces errors.

    Also, printing on the screen isn't considered as returning a value.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    But ffs!!! this is perfectly valid C++ code:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Some people just dont get it...main always returns an int!" << std::endl;
    }
    and so is this:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Again some people just dont get it...main must always return an int!" << std::endl;
        return 0;
    }
    Ok listen to me..if you are so stupid that you dont follow my corrections even after posting about it several times. especially since I have been here for almost 2 years now then I wont tell you any more! Just dont post any source here where you use void main because that will be the first thing people tell you until you change it! And why bother posting here if you cant take advices from those who knows better?
    Last edited by Shakti; 07-23-2005 at 04:57 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Are there new features i'm missing that are in new compilers?

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Well apart from your compiler being one of the worst when it comes to standard compliance nowadays it also has a crappy and buggy implementation of STL and C++ string class.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Where'd you get your compiler? How much?

    How about this?

    http://www.newegg.com/Product/Produc...82E16832102272

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Actually you dont have to buy a compiler, a compiler that is considered to be one of the best out there is MingW, do a search for Bloodshed DevC++ on google and you will get a very good IDE together with a very good compiler.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Quote Originally Posted by Shakti
    Actually you dont have to buy a compiler, a compiler that is considered to be one of the best out there is MingW, do a search for Bloodshed DevC++ on google and you will get a very good IDE together with a very good compiler.
    What about Microsoft Foundation Classes?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM