C++ Namespace
Results 1 to 5 of 5

Thread: C++ Namespace

  1. #1
    Member
    Join Date
    Feb 2003
    Posts
    78

    C++ Namespace

    I am also a C++ newbie. I am workin on C++ in Highschool and we use Turbo version 4.5 C++ Compiler. I have no idea where they found this at, or if it is a pay version. But I cant find a Turbo compiler like it. I have downloaded Dev C++ and almost gave up on it becuz I could not get my simple programs to work. Well I just got done reading the thread Help on Dev C++ and heard about this namesspace and std:: thingy. I did a lil search on the net and looked it up in my Reference book, but both answers were far over my head. I was wondering what it is, and why I have to use it in Dev when trying to use the cout command when I dont have to use it at school. Also wondering gerenal info on the whole library and if there is like a set of libraries all C++ programmers use, becuz our student helper creates his own little libraries for us to use at school. Any info is apperciated.

    -Ep
    01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001

  2. #2
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    A namespace is a definition of code that is constant across most compilers, std being the standard template library, or the standard namespace. Most compilers use their own tweaked version of it by default which can be a pain when using multiple compilers if you don't know the little subtleties to them. To use this namespace you can either add the line "using namespace std" after your includes but before any other code or you can use "std::<function/class>()" for the specific instance you want to use it, for example if you wanted to use the stl version of cout, which usually isn't changed either way, you could use "std::cout << yourstuff;" instead of jus "cout << yourstuff;", I find strings are usually handled differently in the stl than microsoft's standard libraries, I've run into some differences in g++ as well. The big benefit to this is of course that if you work on many different platforms and compilers you can just add that line of code at the top and now you don't need to learn any compiler specific little tricks.
    Reality is the one who has it wrong, not you

  3. #3
    Member
    Join Date
    Feb 2003
    Posts
    78
    Thank you pecosian,
    I got it to compile and run now, but it just flashes. Use the school compiler when we run the program it prints the output in a white box and just stay there until you close it. Using dev, a black bow blinks like my code is running, it prints the ouput, and then close in a sec. This is my exact code.

    //Just a try

    #include <iostream>

    using namespace std;

    int main(void)
    {
    cout<<"Hullo"<<endl;
    cout<<"I see you"<<endl;

    }

    I dont know why it wont stay up there. I modified the examples dev gave me so they are no use. One of the examples they gave me uses a while loop, but I modified it so now it has become an infinate loop. I dont know what to do. Help!

    -Ep
    01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    344
    yeah sometimes stuff like that happens...my Microsoft Visual C++ compiler did that. All you have to do is add something like this
    Code:
    #include <iostream>
    using namespace std;
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main (void)
    {
        std::cout << "Hello" << std::endl;
        std::cout << "I see you" << std::endl;
       
        //to make sure window stays open type...
        char x;
        std::cout << "Press any key to end this program" << std::endl;
        std::cin >> x;  //window stays open until you enter something...
        return 0;
    }
    You can use a using directive, or you can use using declarations....i used both, which is bad programming, but i just wanted to show you that there are multipule ways to do it Also, dont forget to return something!

    About your problem with the while loop...i dont exactly understand what you did, but if you dont want it to run forever, then make it something like this:
    Code:
    #include <iostream>
    using std::cout;
    
    int main (void)
    {
         int x=0;
         while (x<10)
         {
                cout << "I HATE LOOPS, AND THIS WILL ONLY LOOP 10 TIMES";
                std::endl;
                ++x;
         }
    }
    ^^i think that will compile, to lazy and stupid to enter it into my compiler...


    BY THE WAY---> namespaces in C++ are something completly different than what is being explained here. you call a namespace if you want to save a variables name so when another program run on top of your program, the variable names dont get all screwed up. When you are programming a big application you whould use namespaces so 20 different programs dont all have the same variable names. Anyways, someone correct me if i am wrong, because i am sure i said something wrong. Instead of calling this C++ Namespace, you should have titled this C++ Using Namespace Directives and Declarations to better describe the content of this post. Anyways, just trying to help you out
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  5. #5
    Member
    Join Date
    Sep 2003
    Posts
    76
    hey there another way to make sure your program stop if youre using windows is by putting the command

    System("PAUSE");

    this will prevent it from closing till u press something;
    I Speak in frequencies even dogs have trouble hearing


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •