Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I am new to linux environment and I am suing ubuntu 9.04. I wrote a simple hello world c++ program: #include<iostream> int main(void) { cout<<" Hello World! \n"; ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2

    C++ help!

    Hi All,
    I am new to linux environment and I am suing ubuntu 9.04. I wrote a simple hello world c++ program:

    #include<iostream>

    int main(void)
    {
    cout<<" Hello World! \n";
    return 0;
    }



    and executed: gcc -o sample sample.cpp and got the following error:

    sample.cpp: In function ‘int main()’:
    sample.cpp:5: error: ‘cout’ was not declared in this scope


    Please help. I am new to linux and I know C++ very well. I don't know if I have to install any packages to gcc to execute C++ programs. Also, Please guide me as to how to execute java programs in linux and to use netbeans or eclipse in linux to implement java programs.

    Thanks a lot in advance.

    Ashraya.

  2. #2
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,114
    #include<iostream>

    int main(void)
    {
    cout<<" Hello World! \n";
    return 0;
    }
    I am not a programmer, but this works

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main(void)
    {
    cout<<" Hello World! \n";
    return 0;
    }

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,977
    As rcgreen indicated, you are missing telling the compiler that you want to use namespace std. You could alternatively do this:
    Code:
    #include <iostream>
    int main(void)
    {
        std::cout << "Hello World!" << std::endl;
        return 0;
    }
    Note my use of the io manipulator endl. That is preferable in C++ to the use of an embedded newline. It will also make sure that the output is flushed as well as generating the newline.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    19
    C++ requires you to have "using namespace std;" in your code, C & Java do not. Hopefully that help.

Posting Permissions

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