Header files

I am using different header files, something ive never done before, I have always just had everything in the main.cpp but I am wondering, I know I am meant to do the #include "header" file name in the main cpp etc, but I do need to do this for the 2 header files aswel right?

When I am trying to include the header files in each other, I get an error saying that one header file includes itself, which it doesnt, it includes the other header file
Could you please post your code?

If you have two classes, lets say A and B that you implemented in four files lets a.h, a.cpp, b.h and b.cpp, then a.h can not include b.h if b.h also includes a.h. I believe this is because the compiler want to fully define all the included definitions before it continues to complete the current file.
So if a.h includes b.h and b.h includes a.h the compiler would try to do something like:
a.h include b.h -> the compiler tries to fully define b.h, what means that it should:
b.h include a.h -> the compiler tries to fully define a.h, what means that it should:
a.h include b.h -> which is not possible until somewhere after infinity.

To overcome this, you can prioritize your includes using forward declaration:
a.h include b.h
b.h forward declare class A
b.cpp include a.h

in code: b.h
1
2
3
4
5
6
7
// no including a.h here, instead we do a forward declaration. 
// Think of it like a promising "you don't know what class A is yet, but  you will know before you have to use it"
class A;   // forward declaration of a class that we will refer to but will include later.
class B    // start of the scope of class B
{
       // function declarations of class B
};


in code: b.cpp
1
2
3
4
5
6
#include b.h

// first we have to make good on our promise and include a.h 
#include a.h

// implementation of class B functions 
Last edited on
figured it out :)
In header 1 line 5 you include header 2.
In header 2 line 5 you include header 1, but you don't seem to use the Character class at all in that file, so at this moment you could just remove that line. If you want to keep the include because you will use it later, use a forward declaration.

Have a look at this (and read the comments). I hope it will help you to proceed.

main.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<string>
#include<vector>
#include <fstream>
#include "CharacterClass.h"
#include "PokemonClass.h"

using namespace std;

void EnterPokemonDetails();
void MenuDisplay();
void MenuSelection(int PlayerSelection);

void EnterPokemonDetails();
void PrintCharacterDetails();
void MainCharacterCreation();


int main() {
	MenuDisplay();
	system("Pause");
	return 0;
}

//Start Pokemon


void MenuDisplay()
{
	int menuSelection;
	cout << "Welcome to Pokemon" << endl;
	cout << endl << endl;
	cout << "1: Create a Character" << endl;
	cout << "2: Add a Pokemon" << endl;
	cout << "3: Display all of the details of the Character and Pokemon" << endl;
	cout << "4: Save the Game" << endl;
	cout << "5: Load the Game" << endl;
	cout << "6: Exit the Game" << endl;
	cout << "Please make a selection: ";
	cin >> menuSelection;
	MenuSelection(menuSelection);
}

void MenuSelection(int playerSelection)
{
	switch (playerSelection)
	{
	case 1:
		cout << "You selected the make the Main Character screen\n";
		MainCharacterCreation();    // not part of a class so it should probably not be in a header file
		MenuDisplay();
		break;
	case 2:
		cout << "You selected the add a Pokemon\n";
		EnterPokemonDetails();      // not part of a class so it should probably not be in a header file
		MenuDisplay();
		break;
	case 3:
		cout << "You selected Display all of the details of the character and the Pokemon\n";
		PrintCharacterDetails();    // not part of a class so it should probably not be in a header file
		MenuDisplay();
		break;
	case 4:
		cout << "You selected Save the Game\n";
//		PersonCharacterName.SaveCharacter();    you can't use functions from another class without an object of (or pointer to an object of) that class
//		PersonCharacterName.SavePokemon();      you can't use functions from another class without an object of (or pointer to an object of) that class
		Character* tempChar = new Character;   // Creating an object to show how to call the functions. Note that because we used "new" this object will keep existing after break is reached, but if you don't make sure you keep the pointer somewhere you will not be able to delete it and you create a memory leak.
		tempChar->SaveCharacter();
		tempChar->SavePokemon();
		delete  tempChar;                      // prevent memory leak
		MenuDisplay();
		break;
	case 5:
		cout << "You selected Load the Game\n";
//		PersonCharacterName.LoadCharacter();    you can't use functions from another class without an object of (or pointer to an object of) that class
//		PersonCharacterName.LoadPokemon();      you can't use functions from another class without an object of (or pointer to an object of) that class
		Character tempChar;                     // Creating a temporary object to show how to call the functions. Note that the scope of this object is very limited, as soon as the break is reached it will be destroyed. Probably you want it to exist longer.
		tempChar.LoadCharacter();               // Note that the scope of this object is very limited, as soon as the break is reached it will be destroyed. Probably you want it to exist longer.
		tempChar.LoadPokemon();                 // Note that the scope of this object is very limited, as soon as the break is reached it will be destroyed. Probably you want it to exist longer.
		MenuDisplay();
		break;
	case 6:
		cout << "You selected Exit the Game\n";
		break;
	default:
		cout << "You have made an illegal choice\n";
	}
}

void MainCharacterCreation()
{
	string CharacterName;
	cout << "Please enter the characters name: \n";
	cin >> CharacterName;
	PersonCharacterName.setName(CharacterName);
}

void EnterPokemonDetails()
{
	string PokemonName;
	string type;
	Pokemon *p = new Pokemon;
	cout << "Please enter the Pokemons name: " << endl;
	cin >> PokemonName;
	p->setName(PokemonName);
	cout << "Please enter the Pokemons type: " << endl;
	cin >> type;
	p->setType(type);
	PersonCharacterName.add(*p);
}

void PrintCharacterDetails()
{
	cout << "The Characters details are as follows: \n";
	cout << "Your characters name is " << PersonCharacterName.getName() << endl;
	cout << "Your Pokemon detail are as follows: \n"; PersonCharacterName.PrintPokemon();
}


CharacterClass.h
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
#ifndef CHARACTERCLASS_H    // the ifdef comes first, even before the includes
#define CHARACTERCLASS_H

#include<iostream>
#include<string>
#include<vector>
#include <fstream>

using namespace std;

class Pokemon; // if you use forward declaration there can not be any implementation in your header file that uses the forward declared class.
// Personally I think it is best to have no implementation in header files at all because it makes you search more, but for example string Character::getName() could have been implemented here as it does not use a forward declared class.
class Character
{
public:
	Character();
	string getName();
	void setName(string newName);
	void add(Pokemon m);
	void PrintPokemon();
	void SaveCharacter();
	void LoadCharacter();
	void SavePokemon();
	void LoadPokemon();

private:
	vector<Pokemon>CharacterPokemon;
	string Name;
};

#endif // CHARACTERCLASS_H 


PokemonClass.h
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
#ifndef POKEMONCLASS_H    // the ifdef comes first, even before the includes
#define POKEMONCLASS_H

#include<iostream>
#include<string>
#include<vector>
#include <fstream>
// #include "CharacterClass.h"  // you don't use anything from this header yet, so you could just remove the include.

using namespace std;

class Pokemon
{
    public:

        Pokemon();
        string getName();
        void setName(string PokemonName);
        string getType();
        void setType(string PokemonType);

    private:

        string name;
        string type;
};
#endif // POKEMONCLASS_H 


PokemonClass.cpp
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
#include "PokemonClass.h"

Pokemon::Pokemon()
{
}

void Pokemon::setName(string PokemonName)
{
	name = PokemonName;
}

void Pokemon::setType(string PokemonType)
{
	type = PokemonType;
}

string Pokemon::getName()
{
	return name;
}

string Pokemon::getType()
{
	return type;
}


CharacterClass.cpp
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
#include "CharacterClass.h"
#include "PokemonClass.h"   // keep the promise of the forward declaration

Character::Character()
{
}

string Character::getName()
{
    return Name;
}

void Character::setName(string newName)
{
    Name = newName;
}

void Character::add(Pokemon m)
{
    CharacterPokemon.push_back(m);
}

void Character::PrintPokemon()
{
    for (vector<Pokemon>::iterator iter = CharacterPokemon.begin();
        iter != CharacterPokemon.end(); ++iter) {
        cout << iter->getName() << endl;
        cout << iter->getType() << endl;
    }
}

void Character::SaveCharacter()
{
}
void Character::LoadCharacter()
{
}
void Character::SavePokemon()
{
}
void Character::LoadPokemon()
{
}
Last edited on
Topic archived. No new replies allowed.