You are here:

C++/compiling C++ on redhat linux8.0

Advertisement


Question
dear sir,
i tried to compile a simple C++ program on my redhat linux8.0 box.
its not recognising 'cout' command.
is there any modification needed for compiling?
i used command...
1.g++ t.c++


my program t.c++
#include<iostream>
int main(){
  cout<<"hello";
  return 0;
}

could u explain me if any configuration is needed for system, before using g++ compiler?
thanks in advance.


Answer
I am not sure what your current configuration of the GNU C++ compiler is but I think it is probably fairly standard - is it a 3.x version?

If it is compiling your program and giving reasonable errors as it seems to be doing then it is working!

Assuming your g++ version/configuration is sticking fairly well to the C++ ISO/ANSI standard then your problem is that cout is _not_ in the global namespace. It is in the std namespace, along with the majority of the names in the C++ standard library.

Try replacing cout with std::cout to qualify that cout is in the namespace std.

You can also use using declarations or directives. A using declaration that is not a member declaration of a class brings a qualified name into the current 'declarative' scope. For example you could add:

       using std::cout;

To main before you use cout. The effect is that cout is added as a synonym for std::cout to the whole of main, but not outside of main as this is outside the declarative scope in which the using declaration applies.

Alternatively you could use a using directive, which make the compiler look in the namespace the directive names for any unqualified names in addition to the usual places searched. For example you could place:

       using namespace std;

Either in main or more commonly at the top of the source file _after_ any #include pre-processor directives.
Note that the using directive means all seen names in the named namespace will be located when an unqualified name is looked up - so you may be surprised if such names clash with any that your code declares - the compiler will give errors about ambiguities or redefinitions. For this reason you should probably avoid using directives, especially at file scope, except maybe when porting old C++ code that pre-dates the use of namespace std.

Personally I now usually just add the std:: prefix to C++ standard library names and be done with it - after a while you get used to it.

Finally you should _never_ place such using declarations or directives in an include file as they may change the meaning of other code depending on the order include files are included in source modules as they can change what entity a name refers to. This is also why they should appear after all #include directives. See http://www.gotw.ca/, specifically http://www.gotw.ca/gotw/053.htm (Migrating to Namespaces). Much of this article may be of interest, however the specific point about not placing using directives or declarations in header files appears in the solution to question 2, about a third or so down the article. I think the explanation in Herb Sutter's book "More Exceptional C++" is even better.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.