Fedora Linux Support Community & Resources Center
  #1  
Old 18th December 2011, 10:00 PM
johnnytda Offline
Registered User
 
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45
linuxopera
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
Reply With Quote
  #2  
Old 18th December 2011, 10:09 PM
glennzo Offline
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 14,543
linuxfirefox
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 © ®

[SIGPIC][/SIGPIC]
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
Reply With Quote
  #3  
Old 18th December 2011, 10:34 PM
roelj Offline
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Age: 22
Posts: 289
linuxopera
Re: I can not compile .cpp file,error compile

Try

Code:
#include <iostream>
instead.
Reply With Quote
  #4  
Old 18th December 2011, 10:40 PM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,075
windows_xp_2003firefox
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.
Reply With Quote
  #5  
Old 18th December 2011, 11:05 PM
johnnytda Offline
Registered User
 
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45
linuxopera
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??
Reply With Quote
  #6  
Old 18th December 2011, 11:25 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 31
Posts: 2,750
linuxfirefox
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 View Post
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
Reply With Quote
  #7  
Old 18th December 2011, 11:32 PM
johnnytda Offline
Registered User
 
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45
linuxopera
Re: I can not compile .cpp file,error compile

I tried
using namespace std;
and it works
thanks for all
Reply With Quote
  #8  
Old 19th December 2011, 12:24 AM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 31
Posts: 2,750
linuxfirefox
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
Reply With Quote
  #9  
Old 19th December 2011, 12:38 AM
johnnytda Offline
Registered User
 
Join Date: Sep 2010
Location: Romania::Transilvania
Posts: 45
linuxopera
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...
Reply With Quote
  #10  
Old 19th December 2011, 09:32 AM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,075
windows_xp_2003firefox
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.
Reply With Quote
Reply

Tags
compile, cpp, error, file

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to compile .cpp file. mgsrikanth Using Fedora 10 17th April 2011 10:27 PM
can't compile - missing .config file? steveywonder Using Fedora 4 8th March 2009 10:48 PM
How do i compile a source file made with gcc ver 3.3.5 and 4.0.1? tootal2 Programming & Packaging 3 19th October 2008 05:44 PM
xoscope compile problem on F9 - compile with gcc3.3? tmort Using Fedora 0 28th August 2008 04:35 PM


Current GMT-time: 15:45 (Wednesday, 23-04-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat