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";
...
- 12-13-2009 #1Just 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.
- 12-13-2009 #2I am not a programmer, but this works#include<iostream>
int main(void)
{
cout<<" Hello World! \n";
return 0;
}
Code:#include<iostream> using namespace std; int main(void) { cout<<" Hello World! \n"; return 0; }
- 12-15-2009 #3Linux Guru
- 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:
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.Code:#include <iostream> int main(void) { std::cout << "Hello World!" << std::endl; return 0; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 12-15-2009 #4Just 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.


Reply With Quote