who includes who?

This is a discussion on who includes who? within the C++ Programming forums, part of the General Programming Boards category; Currently I am quite confused which file must include which one. (using Visual Studio) If I have in my project ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    who includes who?

    Currently I am quite confused which file must include which one. (using Visual Studio)

    If I have in my project just a single file anyname.h with content
    Code:
    int main() { return 0; }
    [code tags are kinda useless for this very small part but I was forced to] then I can`t compile this. But if there is just a single file with anyname.cpp then it will compile. So I reasoned that the compiler just picks up any file and starts compiling.

    If I have two files a.cpp and a.h it does not matter where is int main() { return 0; } and where is just int main();.

    I think a good way to start is to create main.h and main.cpp. The first thing in main.cpp is #include main.h. But then? Where is best place to include stdlibs such as #include <iostream>? In main.h or main.cpp?

    Also if I add other files such as myfunction.cpp and myfunction.h I have currently no clue who need to include who...

    Can you recommend me anything to read what is the best way to structure this?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You don't put function definitions in headers. Especially not main().

    A file includes exactly those headers it needs. If a.h needs something from <cstdlib> (for example, a type), it will include it. If only a.cpp needs something, that file will include it instead.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    Where to put the definitions? No definitions at all?

    In some tutorial I did read something like "Imagine you want to publish a dll for third party developers. All definitions go to the .h file, the implementations go into the .cpp file. Now you can publish your dll along with the header file and keep your source closed if wanted."

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> All definitions go to the .h file, the implementations go into the .cpp file.
    That should read declarations (or possibly class definitions). Function definitions is what implementation is referring to among other stuff.

    For main, there is no need to declare it in a header file at all. The reason is that you are not allowed to call main yourself, so no other piece of your code needs to see its declaration.

    For other functions and classes, if you want to use them in multiple source (cpp) files, then you should declare them in a header. Otherwise it isn't necessary to declare them in a header then either.

    Generally I might start with a main.cpp only (no main.h). If I need other files I create them as needed file1.h, file1.cpp, file2.h, file3.h, file3.cpp, etc. Except for your main.cpp, almost all your cpp files will correspond to a header file.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    - All function prototypes and class definitions go into headers.
    - All class and function templates go into headers (there may be exceptions, but this is the general way it's done).
    - All header files should include the other headers it needs to work properly (so if a class definition uses another class, it should include that class definition header).
    - All implementation and function definitions go into source files.
    - Source files include appropriate header files to see the declarations and class definitions (and template classes/functions).
    - Source files include appropriate headers like cstdlib when they need them.

    That's about how it works.
    Headers ends with .h (the most common I think) or .hpp.
    All source files ends with .c (for C) and .cpp (for C++).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    so if a class definition uses another class, it should include that class definition header
    if class includes (or function parameter is) pointer or reference to another class - no need to include file with the full class definition, enough to have a forward declaration to avoid unneded header dependencies.

    full heder of the referenced class will be included in source file where the class is actually accessed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile and includes
    By Lang in forum C Programming
    Replies: 4
    Last Post: 03-18-2008, 03:29 AM
  2. Avoiding multiple includes
    By plan7 in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 05:13 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Circular includes
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2004, 05:43 PM
  5. #includes
    By RedLenses in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2002, 02:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21