CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2018
    Posts
    158

    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?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2018
    Posts
    158

    Re: directive #include

    Quote Originally Posted by 2kaud View Post
    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.
    Examples which I'm studying have always 3 files:

    main.cpp
    myfile.h
    myfile.cpp

    myfile.h:
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    myfile.cpp:
    Code:
    #include "compito.h"
    main.cpp:
    Code:
    #include <iostream>
    using namespace std;
    #include "compito.h"

    my file.h
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    const int DIM = 3;
    struct elem{
      int info;
      elem* pun;
    };
    
    class Schedario{
      
      elem* p0[DIM];
      
      void svuota(elem*&); 
      Schedario(const Schedario&);  
      Schedario& operator=(const Schedario&);
    
    public:
      Schedario();    
      void aggiungi(int, int);
      Schedario& operator-=(int);
      friend ostream& operator<<(ostream&, const Schedario&);
        void promuovi(int,int);
      ~Schedario();
    };
    Why my file.h needs cstring include? This is necessary because a function inside miofile.cpp needs cstring? If yes, this way is to keep all include into unique file?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2018
    Posts
    158

    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 ?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: directive #include

    Quote Originally Posted by zio_mangrovia View Post
    #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?
    Victor Nijegorodov

  8. #8
    Join Date
    May 2018
    Posts
    158

    Re: directive #include

    I don't know difference. I'm using G++ compiler in Linux environment (Debian) by command line interface.
    This Forum is not right?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: directive #include

    Managed c++ is very different. Post in the non-visual c++ forum. http://forums.codeguru.com/forumdisp...sual-C-Issues)

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: directive #include

    [moved from Managed C++ and C++/CLI forum]
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured