LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2007, 08:03 PM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
Problem with hello world


I ran this by using: gcc hello.cpp

Quote:
#include <iostream>

int main()
{
cout << "Hello World!" << endl;
return 0;
}
I got this error:

Quote:
hello.cpp: In function `int main()':
hello.cpp:5: `cout' undeclared (first use this function)
hello.cpp:5: (Each undeclared identifier is reported only once for each
function it appears in.)
hello.cpp:5: `endl' undeclared (first use this function)
I cant find any mistake in this simple program.
 
Old 02-21-2007, 08:26 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Take a look at this thread. In short:

Code:
$ cat hello.cpp 
#include <iostream>

using std::cout;
using std::endl;

int main()
{
cout << "Hello World!" << endl;
return 0;
}


$ g++ hello.cpp
$
 
Old 02-21-2007, 10:34 PM   #3
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Ah, that worked.Thanks for the link.

One question: Why should I insert "using namespace std"? Can't I just omit it? This program worked properly without it in windows.
 
Old 02-21-2007, 10:44 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
From the link:

Quote:
this is why #include <stdlib> does not work in C++. The latest restandardization of C++ has a part that pretty much says that all the include files are antiquated. That's why <iostream.h> is <iostream>. With that came the changing of C headers. In this case, #include <stdlib.h> becomes <cstdlib>. Don't ask why, I don't know =].

As for 'cout undeclared', that's also because of the most recent restandardization. If your studies of C++, you'll eventually read about namespaces. Namespaces are more or less your own way of scoping variables. All of the built in classes (cout, cin, endl, vector, string, etc.) are in the namespace std. So to use them you have to use :: operator since all those classes are no longer in a global scope. (Ah OOP.... i hate it sometimes)
 
Old 02-21-2007, 10:50 PM   #5
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
Ok, Thank you for the help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
emerge -u world problem matrixon Linux - Software 4 03-06-2006 09:24 PM
Hello World problem tayolinux Programming 5 07-28-2004 04:17 PM
Hello world problem devit Programming 2 05-08-2004 01:28 PM
QT Hello world linker problem digitized_funk Programming 0 10-19-2003 02:30 AM
Small hello world problem, again!, sorry. scatcat Programming 4 04-05-2002 02:45 AM


All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration