[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppInclude

(C++) #include

A preprocessor statement to add a header file (.h or .hpp) or other files to your program.

There are three different header file #includes:

  1. include <iostream> // 1: STL
  2. include <assert.hpp> // 2: The standard header file directory
  3. include "Unit1.h" // 3: Local


The first, without the .h extension, means that this header file is from the Standard Template Library. The second #include means that the header is not from the STL but in the standard header file directory. The third #include means that the file is local, i.e. in the same directory as the program. If the local #include fails, the standard header file directory is checked for this header file.

The C style include on  ?CppStl STL headers

For backwards compatibility with C one can [[CppInclude
    1. include]] C++ header files with the .h extension.
  1. include <stdio.h> //C-stle #include !!!


This header file is a wrapper: all it does is call the C++ header file cstdio and then adds a 'using namespace std', as C does not have namespaces.

This has the unfortunate side-effect that after calling such a header file all functions and classes in namespace std will be in the global namespace.

As it pollutes the global namespace, avoid using namespace std [4,5].

An example from [6]:

  1. include <vector> //Carefully avoids polluting the global namespace
vector v1; //Error: no 'vector' in global namespace
  1. include <stdio.h> //Contains a 'using namespace std'.
vector v2; //Oops: this now works


Implementation (.c or .cpp) files

These normally are not #included. Instead, these are added to your project. For example, in C++ Builder, select 'Project | Add to Project'.

Forgetting to add an implementation file to you project results in a linking error.

'#include' links

References



last edited (December 3, 2006) by bilderbikkel, Number of views: 5283, Current Rev: 9 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.