Spliiting code into headers

closed account (y0XSE3v7)
I just learned how and why to split files into headers. Used it, it made sense and made programming much easier.
My question is after I have split my code into many header files like the example I was playing around with below, when I want to use this header that is made of many other headers do I have to put all its files and itself into the directory of the code that I am going to use it in or is there an easier, better standard way of doing this?

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
#ifndef MATRIX_H
#define MATRIX_H

#include <vector>
#include <cmath>
#include <iostream>
#include <fstream>

template <class T>
class matrix
{
    public:

    int Rows;
    int Columns;
    std::vector < std::vector <T> > Element;

    //Constructors
    matrix ();
    matrix (int rows,int columns);
    void Redefine(int r,int c);

    //Operators
    double & operator ()(int r, int c){return Element[r][c];}

    //Sum of matrices
    matrix operator + (matrix A);
    matrix operator - (matrix A);

    //Matrix multiplication
    matrix operator * (matrix A);

    //Input & Output
    void Read(std::fstream & A);
    void Write(std::fstream & A);
    void Disp();
};

#include "matrix_constructors.h"
#include "matrix_sums.h"
#include "matrix_multiplication.h"
#include "matrix_specials.h"
#include "matrix_rw.h"

#endif
Last edited on
usual practice is to include your own headers at the begining of header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MATRIX_H
#define MATRIX_H

// system headers
#include <vector>
#include <cmath>
#include <iostream>
#include <fstream>

// my own headers
#include "matrix_constructors.h"
#include "matrix_sums.h"
#include "matrix_multiplication.h"
#include "matrix_specials.h"
#include "matrix_rw.h"

// my declarations ... ( not definitions)

#endif // MATRIX_H  


do I have to put all its files and itself into the directory of the code that I am going to use it in or is there an easier


Yes because your headers are included with "" quotes #include "matrix_sums.h"

when included with quotes source files will look for your headers in the same directory, if not found you'll get an error.

if you have your headers somewhere else use full paths to header files:
#include "C:\path\to\headers\matrix_constructors.h"

alternatively you can avoid specifying full path, and instead go to compiler options and tell your compiler where to look for headers.
Last edited on
closed account (y0XSE3v7)
Okay thanks for that you have really cleared that up for me, what if I want to use matrices.h (which is the file that contains the code above) in a program in a different directory, is it enough for me to include matrices.h with it's directory or do I have to do something about the headers that are included in it too.
what if I want to use matrices.h (which is the file that contains the code above) in a program in a different directory


you can do this, but you also need to modify compiler options > include directories and input directory names containing all the headers that matrices.h includes.

option 2 is to use full include paths in
matrices.h
as explained in first post.
closed account (y0XSE3v7)
I think I will go for the second option you suggested, thank you for all your help.
Last edited on
Topic archived. No new replies allowed.