If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

directive #include

#include directive is used to retrieve functions headers, where are declared in, into .cpp file. Right?
So if I have 2 .cpp files (a.cpp , b.cpp) and if I used 'cout' function in a.ccp I will need #include <iostream>, right?
If I created my file.h ? I have to specify into it, the header files?

Re: directive #include

If file.h uses cout, then file.h should have #include <iostream>. Note that in headers it is not considered good practice to use a using namespace ... statement.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: directive #include

Before a function is used, the compiler needs to know how it is declared. If the function hasn't been defined before the call, then a 'forward declaration' of the function is used. These forward declarations for standard functions are in the various #include files and are included into a program as needed. There is no problem with including files that aren't needed.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: directive #include

But if header file (eg test.h) contains only function declarations and these functions are defined into test.cpp, where are for example strcmp() or cout ... where I have to write #include <iostream> and #include <cstring> ? In .h or .cpp ?

Re: directive #include

The code for strcmp() is in a .dll which is linked with your program when it's compiled and is part of a set of .dlls known as the C Run Time Library (CRT). These CRT .dll files need to be on any computer that runs c++ programs - and are part of a standard Windows installation.

cout is declared in iostream.

#include files can contain any valid code - not just function declarations. The c++ STL is implemented as code in multiple include files. Include files can also have #include statements etc.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: directive #include

Originally Posted by zio_mangrovia

#include directive is used to retrieve functions headers, where are declared in, into .cpp file. Right?
So if I have 2 .cpp files (a.cpp , b.cpp) and if I used 'cout' function in a.ccp I will need #include <iostream>, right?
If I created my file.h ? I have to specify into it, the header files?

You posted in the Managed C++ and C++/CLI forum.
But is your code a managed C++? Or is it a native one?

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.