I've only recently began splitting my projects into separate source and header files, and running into regular linking errors.
All is fine putting function declarations in a single header.h, and defining them each in their own .cpp file.
But I always run into problems when I have more than one header file, for instance when I write a class.
So for instance, say I have:
Main.cpp
Functions.cpp // Some function definitions
Header.h // Some function declarations
Player.h // A class with player stats
In my header.h file, I have a function declaration that takes a Player object as an argument, something like:
void drawBoard(char arr[ROW][COL], Player& hero);
This produces a syntax error, presumably because it can't "see" the player class. So I add
#include "Player.h"
to the header.h
This seems to remedy the visibility of the class for the function definition (the red squiggly line is no more in VS!), but now I get a "error LNK2005: Player::Player(void) already defined", presumably because now in every subsequent .cpp file that has #include.h, the class is being defined multiple times. At least that is my understanding.
So what is the general way of doing this? Should a header file never include another header file? Should you have a main header, such as Header.h, that links everything together, and only ever have an #include "header.h" in your .cpp files?
Here's the structure of some code I was practising with, which gives me the above link error.
PLAYER.H
1 2 3 4 5 6 7 8 9 10 11 12
|
#ifndef PLAYER_H
#define PLAYER_H
#include "Header.h"
class Player
{
// Some player stats. Functions declared
// and defined in the same file
}
#endif
|
HEADER.H
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include "Player.h"
static const int ROW = 3;
static const int COL = 3;
void initBoard(char (&arr)[ROW][COL]);
void drawBoard(char arr[ROW][COL], Player hero);
#endif
|
DRAWBOARD.CPP
1 2 3 4 5 6 7
|
#include "Header.h"
#include "Player.h"
void drawBoard(char arr[ROW][COL], Player hero)
{
// Code to display board
}
|
INITBOARD.CPP
1 2 3 4 5 6
|
#include "Header.h"
void initBoard(char (&arr)[ROW][COL])
{
// Code to initialise board
}
|
MAIN.CPP
1 2 3 4 5 6 7 8 9 10 11 12
|
#include "Header.h"
//#include "Player.h"
int main()
{
Player hero;
char board[ROW][COL];
initBoard(board);
drawBoard(board, hero);
}
|