Thread: Organizing files/function in header files.

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    Organizing files/function in header files.

    Hi all,

    if i have the following
    a header file called "temp.h" :
    Code:
    #ifndef TEMP_H
    #define TEMP_H
    double to_celsius(double fahrenheit);
    #endif
    the temp.cpp file :
    Code:
    #include "temp.h"
    double to_celsius(double fahrenheit){
          double celcius = (fahrenheit - 32) * 5.0 / 9.0;
          return celcius;
    }
    the main.cpp:
    Code:
    #include "temp.h"
    int main() {
        cout << to_celsius(32) << endl;
        return 0;
    }
    My question, is it necessary to include the header in "temp.cpp" and "main.cpp" ? or is it enough only "main.cpp" (because it works for me when i include it only in main.cpp).

    are there different cases? or is there some best practice?

    any hints or links to read more about it is appreciated

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to include the header file in main.cpp because to_celsius must be declared before it is called in the main function.

    You don't need to include the header file in temp.cpp in this case, but you should because that way the compiler will warn you if there's a mismatch between the function declaration and its definition. In other cases, there may be other things declared in the header that you will want to use in temp.cpp
    Last edited by laserlight; 09-20-2020 at 02:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    alright, got it, thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Organizing and optimizing code into source and header files
    By edishuman in forum C++ Programming
    Replies: 3
    Last Post: 01-24-2012, 08:53 AM
  2. function pointers and header files and such
    By crypticgeek in forum C Programming
    Replies: 6
    Last Post: 10-24-2006, 09:11 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Suggestions on Organizing Files
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-09-2004, 01:15 AM
  5. Function and header files
    By MrJake in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2002, 05:22 AM

Tags for this Thread