PDA

View Full Version : Error in Hello World program!?! C++



navneeth
December 25th, 2006, 08:04 PM
This is probably the strangest thing I've seen...I get errors when the hello world program is compiled. I have installed g++.

Here's what I started with:


#include <iostream.h>

void main()
{
cout << "Hello World!";
}


(I tried with both 'main' and 'void main')

This is the error message I got:


In file included from /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31,
from hello.cc:1:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.


And when I remove the 'h' from iostream.h, I got:


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


I have tried using tested programs from the net, but the result is the same. I even tried renaming the extension (cc, cpp, c++).

Any ideas?

Verminox
December 25th, 2006, 08:22 PM
"cout" is an object in the namespace "std".

Either try:


std::cout << "Hello, World!";


or the "using" keyword to tell the compiler that you are using a particular namespace. eg.


using namespace std;
cout << "Hello, World!";

navneeth
December 25th, 2006, 08:27 PM
Thanks. std::cout works, but I see many programs without it. http://www.roesler-ac.de/wolfram/hello.htm#C++

Is it required by the latest vresion of the compiler or something like that?

engla
December 25th, 2006, 08:50 PM
yep, your hello world code is wrong, and I don't know what but some windows environment seems to do "using namespace std" by default.

here's a better version from wikibooks:


#include <iostream>

int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}

note that main must return int.

Jucato
December 26th, 2006, 12:02 AM
One of the problems in your code is #include <iostream.h>. Standard C++ doesn't declare standard header files like that. Instead they used #include <iostream> only (no .h).

Another problem, although not stated in the error message you posted, is that you didn't use the proper namespace for cout and endl. The proper way to use those would be engla's example.

po0f
December 26th, 2006, 01:04 AM
navneeth,

The thing with finding C++ tutorials on the internet is that some of them might come from before C++ was standardized or they might not be too clear if they are targeted at Windows developers and not more generic, C++98 (the standard) programming. I found the Thinking in C++ (http://mindview.net/Books/TICPP/ThinkingInCPP2e.html) books to be good introductions to standard C++ programming (there are no vendor-specific details in it).

navneeth
December 26th, 2006, 06:50 AM
Thanks for the helpful comments, guys, and, po0f, thanks for the link.

I'm just a beginner, and I think the book I use for a course may be seriously outdated.

po0f
December 26th, 2006, 07:14 AM
navneeth,

Most college books tend to be. ;)

Desi-Tek.com
December 26th, 2006, 10:53 AM
i learned c++ from cplusplus.com best online turorial on c++