header guards

should i need to include standard libraries in header files?

example
standard calculator.h
1
2
3
4
5
6
7
8
9
#ifndef STANDARD_CALCULATOR_H_INCLUDED
#define STANDARD_CALCULATOR_H_INCLUDED
#include <iostream>
#include <cmath>
using namespace std;



#endif // STANDARD_CALCULATOR_H_INCLUDED 

or should just include it in the main cpp in which where this header file will include?
because it already has guards?
Last edited on
should i need to include standard libraries in header files?
Yes, every header should include every header it relies upon. You should be able to include only this header and be able to compile your program.
@MiiNiPaa
wont it be redundant?
including a standard library in header file plus the same library in main?
Not necessarily. If you include the same header twice, include guards will prevent that, so all that will happen is that compile times will become microscopically longer.

However, a header might need a certain other header (e.g. <string>) that another source file doesn't need, and hence doesn't include. This will mean that you will get an error on compilation. In general, include EVERY header file that you need, but make sure not to randomly include things that you don't need. For example, you don't often need the <iostream> header in a header file.

On a side note, avoid using namespace std; in header files. Its generally bad practice, it forces the std namespace into the global one, whether the user of the header wants it or not. This defeats the purpose of namespaces.
Topic archived. No new replies allowed.