Help - Search - Members - Calendar
Full Version: C++ Compilation
Linuxhelp > Support > Technical Support
beni
I've RHL 9 (kernel 2.4.20) and the standard gcc/g++ that ships with the distro. I included the iostream header as <iostream.h>, and got an error msg from g++ about some deprecation. Then I changed the header file to <iostream>. Then the compiler fasils to ercognise the cout function. Now can please anyone tell me how to compile a C++ program using g++?
michaelk
should be:
#include <iostream>
using namespace std;

If I remember it was just a warning not an error if you used iostream.h instead of iostream.
Hemant
Yeah...exactly..new ISO c++ standards suggest following way of including header files..
#include <iostream>

thus the old style is deprecated..you can use it..but it means that support will be removed from future versions of g++.
now the second line...
using namespace std;
is required because...keywords like..cout,endl,cin are not in the current namespace...thus compiler will not recognise these keywords..unless you specify which namespace to use.
thus you can have two styles of coding...
CODE
#include <iostream>
using namespace std;
int main()
{
       cout<<"Hello World"<<endl;
       return 0;
}


or the other style could be like..this..
CODE
#include <iostream>
int main()
{
    std::cout<<"Hello World"<<std::endl;
    return 0;
}


or another style code could be..
CODE
#include <iostream>
using namespace std::cout;
using namespace std::cin;
using namespace std::endl;
int main()
{
   cout<<"Hello World"<<endl;
   return 0;
}


The idea behind this style is...you don't use the entire namespace..but only these three keywords..will belong to std
namespace.
beni
Thnx guys. The help is really appreciated smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.