 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

18th December 2011, 10:00 PM
|
|
Registered User
|
|
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45

|
|
|
I can not compile .cpp file,error compile
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
|

18th December 2011, 10:09 PM
|
 |
Un-Retired Administrator
|
|
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 13,934

|
|
|
Re: I can not compile .cpp file,error compile
More of a guess than anything else, but maybe you need to install compat-gcc ??
__________________
Glenn
The Bassinator © ®
Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
|

18th December 2011, 10:34 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Location: Netherlands
Age: 21
Posts: 279

|
|
|
Re: I can not compile .cpp file,error compile
Try
Code:
#include <iostream>
instead.
|

18th December 2011, 10:40 PM
|
|
Registered User
|
|
Join Date: Apr 2005
Location: Finland
Posts: 5,076

|
|
|
Re: I can not compile .cpp file,error compile
The program you are trying to compile isn't valid Standard C++. Try to find a C++ textbook or reference manual published after 1998.
|

18th December 2011, 11:05 PM
|
|
Registered User
|
|
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45

|
|
|
Re: I can not compile .cpp file,error compile
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??
|

18th December 2011, 11:25 PM
|
|
Official Gnome 3 Sales Rep. (and Adminstrator)
|
|
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,711

|
|
|
Re: I can not compile .cpp file,error compile
You also need to use "using std::cout;" etc. or "using namespace std;", or "std::cout << ... << std::endl;", e.g.
Code:
#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 ----------
Quote:
Originally Posted by johnnytda
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
|

18th December 2011, 11:32 PM
|
|
Registered User
|
|
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45

|
|
|
Re: I can not compile .cpp file,error compile
I tried
using namespace std;
and it works
thanks for all
|

19th December 2011, 12:24 AM
|
|
Official Gnome 3 Sales Rep. (and Adminstrator)
|
|
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,711

|
|
|
Re: I can not compile .cpp file,error compile
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
|

19th December 2011, 12:38 AM
|
|
Registered User
|
|
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45

|
|
|
Re: I can not compile .cpp file,error compile
Yes, I teach after Jams's C / C + + Programmer's Bible by Kris Jams and Lars Kland...
|

19th December 2011, 09:32 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Location: Finland
Posts: 5,076

|
|
|
Re: I can not compile .cpp file,error compile
According to Amazon, 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++ by Bjarne Stroustrup. If you are a programmer that wants to learn C++ as a new programming language, see Accelerated C++ by Koenig and Moo.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 07:35 (Saturday, 25-05-2013)
|
|
 |
 |
 |
 |
|
|