16.3) Library Header File
Suppose we wish to distribute the three functions from the statistical analysis program as a single library. We can create the following header file called data.hpp for this library:
1 #ifndef DATA_HPP 2 #define DATA_HPP 3 4 #include <iostream> 5 #include <vector> 6 7 void read_data(std::istream&, std::vector<double>&); 8 double mean(const std::vector<double>&); 9 double stdev(const std::vector<double>&); 10 11 #endif
- Lines 1, 2 & 11 are an ‘include guard’ - ensuring that lines in between will be seen the first time that this header file is included and ignored on any subsequent includes; an alternative approach, supported by most compilers, is to use
#pragma once
at the start of the file - Lines 4 & 5 ensure that the compiler has seen definitions of
istream
andvector
, which is necessary for the prototypes on lines 7–9 make sense - Note the use of
std::
here; we don’t want to put ausing namespace std
in the header file because it would impose that decision on every user of the library