header file and cpp's

example:
main.cpp
1
2
3
4
5
6
7
 
#include <iostream>
#include "board.h"
#include "board.cpp"
int main() {
//function calls
}

board.h
1
2
3
4
5
 
#ifndef BOARD_H_INCLUDED
#define BOARD_H_INCLUDED
//function declarations
#endif 

board.cpp
1
2
3
4
// first question, should i need to include iostream? since i already included it on main.cpp?
void func() {
std::cout << "aw" << std::endl;
}

, do i really need to include cpp files on main function?
and should i include iostream on board.cpp instead on main?
Last edited on
do i really need to include cpp files on main function?

Does main.cpp use any iostream functions such as cout? If so, then yes you need <iostream>. If not, you don't need it.

should i include iostream on board.cpp instead on main

board.cpp clearly uses cout, so yes you need it.
board.cpp and main.cpp are separate compilations. board.cpp would know nothing about whether <iostream> was included in main.cpp or not.

okay i get it all.
last,
is it alright that i include the board.h in board.cpp?
then the only library or header included in main.cpp is board.cpp.
like this:
main.cpp
1
2
#include "board.cpp"
 

board.h
1
2
3
4
5
 
#ifndef BOARD_H_INCLUDED
#define BOARD_H_INCLUDED
//function declaration
#endif 

board.cpp
1
2
3
#include <iostream>
#include "board.h"
std::cout << std::endl;
Last edited on
> do i really need to include cpp files on main function?
no, don't include *.cpp
you may have redefinition errors, and kill the purpose of a linker (having to recompile all the files if you make a modification in any file)

Instead, add the *.cpp to the project..
(undefined reference http://www.cplusplus.com/forum/general/113904/#msg622055 )


> is it alright that i include the board.h in board.cpp?
each source should be self contained (can be compiled separately)
each header should be self contained (don't need to add other headers prior or after it)
Last edited on
Topic archived. No new replies allowed.