PDA

View Full Version : I can not compile .cpp file,error compile


johnnytda
18th December 2011, 10:00 PM
Hi! I have a problem with g + + compiler.
When I try to compile one cpp file from terminal, the following error message appears:

[johnny@fedora-system ~]$ g++ HelloWorld.cpp -o helloworld
HelloWorld.cpp:2:23: fatal error: iostream.h: No such file or directory
compilation terminated.


Here is the source code file from HelloWorld.cpp:

#include <iostream.h>

int main()

{

cout << "Hello World!" << endl;

return 0;

}

Please help me.Thanks

glennzo
18th December 2011, 10:09 PM
More of a guess than anything else, but maybe you need to install compat-gcc ??

roelj
18th December 2011, 10:34 PM

Try

#include <iostream>

instead.

markkuk
18th December 2011, 10:40 PM
The program you are trying to compile isn't valid Standard C++. Try to find a C++ textbook or reference manual published after 1998.

johnnytda
18th December 2011, 11:05 PM
I tried with # include <iostream>, but the following error message appears:
[johnny@fedora-system ~]$ g++ HelloWorld.cpp -o helloworld
HelloWorld.cpp: In function ‘int main()’:
HelloWorld.cpp:8:2: error: ‘cout’ was not declared in this scope
HelloWorld.cpp:8:2: note: suggested alternative:
/usr/lib/gcc/i686-redhat-linux/4.6.2/../../../../include/c++/4.6.2/iostream:62:18: note: ‘std::cout’
HelloWorld.cpp:8:28: error: ‘endl’ was not declared in this scope
HelloWorld.cpp:8:28: note: suggested alternative:
/usr/lib/gcc/i686-redhat-linux/4.6.2/../../../../include/c++/4.6.2/ostream:543:5: note: ‘std::endl’

for markkuk:
markkuk says: The program you are trying to compile isn't valid Standard C++.
then what is wrong with this code??

Gareth Jones
18th December 2011, 11:25 PM
You also need to use "using std::cout;" etc. or "using namespace std;", or "std::cout << ... << std::endl;", e.g.

#include <iostream>

using namespace std;

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

return 0;
}

Gareth

---------- Post added at 11:25 PM ---------- Previous post was at 11:21 PM ----------

markkuk says: The program you are trying to compile isn't valid Standard C++.
then what is wrong with this code??

It's pre-standard C++.

The first and current C++ standard, C++98, moved the standard library classes, objects, functions, templates etc. into the "std" namespace (see the code I already posted) and dropped the ".h" suffix for header files. C++ headers have the old names minus ".h", and ISO C library headers have the names minus ".h", prefixed with "c", i.e.
"stdlib.h" -> "cstdlib". Irritatingly, this doesn't include POSIX C library features, which are still in the global namespace.

Gareth

johnnytda
18th December 2011, 11:32 PM
I tried
using namespace std;
and it works
thanks for all

Gareth Jones
19th December 2011, 12:24 AM
Okay, so you've got g++ working with a trivial example. But in general, getting pre-standard code to work will be harder than merely inserting "using namespace std;" at the start of every file, and even when it works, that is often not the best solution in real code (otherwise they would have just used the global namespace in the first place!).

If you're planning to learn C++, make sure your learning materials are up-to-date, at least newer than the C++98 standard! Beware that the second C++ standard (C++11) is gradually being supported by compilers too, but not uniformly, so it's probably possible to have too-up-to-date learning material too at the moment...

Gareth

johnnytda
19th December 2011, 12:38 AM
Yes, I teach after Jams's C / C + + Programmer's Bible by Kris Jams and Lars Kland...

markkuk
19th December 2011, 09:32 AM
According to Amazon (http://www.amazon.com/Jamsas-C-Programmers-Bible/dp/1884133258), that book is from 1997 so it's probably obsolete. If you are a beginner in programming (not just C++), see Programming: Principles and Practice Using C++ (http://www.amazon.com/Programming-Principles-Practice-Using-C/dp/0321543726/ref=pd_sim_b_9) by Bjarne Stroustrup. If you are a programmer that wants to learn C++ as a new programming language, see Accelerated C++ (http://www.amazon.com/Accelerated-C-Practical-Programming-Example/dp/020170353X/ref=pd_sim_b_3) by Koenig and Moo.