Friend Function help

Ok so in my program im trying to use private variables without making them public so i have a function that accesses the variables but i cant seem to figure out how to do it.

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
#include <iostream>
#include "Player_Class.h"
#include "Weapon_Class.h"
#include "Prototypes.h"

using namespace std;

Player::Player(string PLAYERNAME, bool ISFIRSTSTARTUP): playerName(PLAYERNAME),
                                                        isFirstStartup(ISFIRSTSTARTUP)
{

}

Player::~Player()
{

}

void AccessClasses()
{
    CheckStartup(bool isFirstStartup);
    SaveGame(bool isFirstStartup, string name, string weaponType, string ammunitionType, int ammunitionPrice);
    LoadGame(bool isFirstStartup, string name, string weaponType, string ammunitionType, int ammunitionPrice);
}

int main()
{
    Player player("Default", false);
    Weapon weapon;

    SaveGame(player, weapon);

    return 0;
}


Player_Class.h

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

#include <string>

class Player
{
    public:
        Player(std::string PLAYERNAME, bool ISFIRSTSTARTUP);
        ~Player();
        friend void AccessClasses();

    private:
        std::string playerName;
        bool isFirstStartup;
};

#endif // PLAYER_CLASS_H_INCLUDED 



Weapon_Class.h

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

#include <string>

class Weapon
{
    public:
        Weapon();
        ~Weapon();
        friend void AccessClasses();

    private:
        std::string weaponType;
        std::string ammunitionType;
        int ammunitionPrice;
};

#endif // WEAPON_CLASS_H_INCLUDED 



Prototypes.h

1
2
3
4
5
6
7
8
9
10
11
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

#include "Weapon_Class.h"

void CheckStartup(Player player, Weapon weapon);
void SaveGame(Player player, Weapon weapon);
void LoadGame(Player player, Weapon weapon);
void AccessClasses();

#endif // PROTOTYPES_H_INCLUDED 


GameSetup.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Player_Class.h"
#include "Weapon_Class.h"
#include "Prototypes.h"

using namespace std;

void CheckStartup(Player &player, Weapon &weapon)
{
    LoadGame(player, weapon);

    player.isFirstStartup;
}



SaveAndLoad.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include "Player_Class.h"
#include "Weapon_Class.h"

using namespace std;

void SaveGame(Player &player, Weapon &weapon)
{
    ofstream SaveFile;
    SaveFile.open("Data.txt");

    player.isFirstStartup = true;

    SaveFile << player.playerName << endl;
    SaveFile << player.isFirstStartup << endl;
}

void LoadGame(Player &player, Weapon &weapon)
{
    ifstream LoadFile;
    LoadFile.open("Data.txt");
}


C:\Users\Chay Hawk\Desktop\Gun Shop\Player_Class.h||In function 'void CheckStartup(Player&, Weapon&)':|
C:\Users\Chay Hawk\Desktop\Gun Shop\Player_Class.h|15|error: 'bool Player::isFirstStartup' is private|
C:\Users\Chay Hawk\Desktop\Gun Shop\GameSetup.cpp|12|error: within this context|
C:\Users\Chay Hawk\Desktop\Gun Shop\GameSetup.cpp|12|warning: statement has no effect [-Wunused-value]|
||=== Build finished: 2 errors, 1 warnings (0 minutes, 0 seconds) ===|
1
2
3
4
5
6
7
8
9
void AccessClasses()
//A function that does not take any parameters and returns nothing
//¿how do you expect to work with it?
{
    CheckStartup(bool isFirstStartup);
    SaveGame(bool isFirstStartup, string name, string weaponType, string ammunitionType, int ammunitionPrice);
    LoadGame(bool isFirstStartup, string name, string weaponType, string ammunitionType, int ammunitionPrice);
   //¿trying to "fix" compile errors, I guess?
}


1
2
3
4
5
6
7
8
9
10
class Player{
   friend void AccessClasses();
};

void SaveGame(Player &player, Weapon &weapon)
{
//...
    SaveFile << player.playerName << endl;
    SaveFile << player.isFirstStartup << endl;
}
Read, `Player' considers `AccessClasses()' his friend
¿how does `SaveGame' enter the picture?


¿do you realize that void SaveGame(Player player, Weapon weapon)
and void SaveGame(Player &player, Weapon &weapon) are different?
Last edited on
so, what are you trying to do?

Your AccessClasses() doesn't make sense this way.
Topic archived. No new replies allowed.