Using header files for functions (a question of style)

I have a bunch of different functions that takes a few hundred rows each, so I like to have them in different files. If I understand correctly it's bad style to include .cpp files directly (or is it even more serious)? So I use header-files, one for each function. How about putting all the functions in the same header-file? Is that also bad? They don't have any obvious connection with eachother. But it feels unnecessary to create two files/function.

How do u pros usually deal with this?

Cheers,

Peter
Yes, you generally don't. You generally put all the definitions in a header file, and the code in the .cpp file:

func.h
 
void myfunc(int x);


func.cpp
1
2
3
void myfunc(int x) {
    //do stuff
}


main.cpp
1
2
3
4
5
#include "func.h"

int main() {
    //use myfunc()
}
How about putting all the functions in the same header-file? Is that also bad? They don't have any obvious connection with eachother.


The underlined bit is what concerns me a little bit.

Multiple unrelated global functions doesn't seem to happen much/at all in my experience. This tells me you have some other kind of design problem.

What functions do you have?


1 function per header is a bad idea. I'd put them all in the same header and do as firedraco illustrated (#include the header, but don't #include cpp files!)
"I have a bunch of different functions that takes a few hundred rows each, so I like to have them in different files."
As a programming note for style- your functions shouldn't be hundreds of lines long- can't you break them down into smaller components? this will save you debugging time & make your code potentially faster & more readable.

"So I use header-files, one for each function. How about putting all the functions in the same header-file?"
Header files by definition include: a comment that has all the function prototypes with preconditions & postconditions listed directly below them. The point of this is that any user should be able to run your code or create their own main() function using your functions by reading the comments alone. See sample below.

Is that also bad? They don't have any obvious connection with eachother. But it feels unnecessary to create two files/function. How do u pros usually deal with this?
Let's say you don't have a class or a namespace- but you have a series of functions you have written that you want to be accessible to you when building & compiling different codes (functions that are multipurpose) you could create a library. Although- this is usually done with functions that are all similar. But, like I mentioned earlier, you could probably break down you hundreds of lines per function into a series of smaller functions all relating to the same thing.

Here is an shortened example of a header file I have written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef FWI_H_
#define FWI_H_

/*
CLASS PROVIDED: FireWeatherWRF

CONSTRUCTORS for the FireWeatherWRF class:
   FireWeatherWRF()
      Postcondition: a two dimensional array of size [264][345]
        initialized to zero.

   FireWeatherWRF(double initializer)
      Postcondition: a two-dimensional array of size [264][345]
        intialized to the initializer argument.

MODIFICATION MEMBER FUNCTIONS for the FireWeatherWRF class:
   void read_WRF(charptr file,int variable)
      Precondition: file is a WRF netcdf output file & variable is an
        integer of specifying the weather data expected from the WRF netcdf
        file.Integer codes are: 0=latitude, 1=longitude, 2=rain, 3=wind,
        4=temperature, 5=relative hummidity
      Postcondition: data array is modified to contain variable data
        from the WRF netcdf file for the YYYYMMDDHH specified in the
        file name

CONSTANT MEMBER FUNCTIONS for the FireWeatherWRF class:
   bool is_loaded()
      Postcondition: states whether or not the netcdf data is loaded
        into the constructor
   
   long YYYYMMDDHH()
      Postcondition: returns the date and hour of the file in the format YYYYMMDDHH
 */

typedef char *charptr;
typedef double *dblptr;

namespace FAFS_FWI{
  class FireWeatherWRF{
  public:
    //CONSTRUCTORS
    FireWeatherWRF();
    FireWeatherWRF(double initializer);
    
    //MODIFICATION MEMBER FUNCTIONS
    void read_WRF(charptr file,int variable);
    
    //CONSTANT MEMBER FUNCTIONS
    bool isLoaded();
    long YYYYMMDDHH();
    
    //DESTRUCTOR
    ~FireWeatherWRF();
    
  private: 
    static const int BT=37,WE=345,SN=264;
    bool loaded;
    char date[10];
  };


}

#endif /*FWI_H_*/



Cheers,

Tash
Thanks a lot! interesting to see some real code :). The functions are not totally unrelated. But say one is for input, a couple for doing some calculations in different areas of the problem and then output. So if I I want to re-use some of tem it's unikely I want all. Anyway I decided to put them in the same header for now.
Topic archived. No new replies allowed.