Question about classes

I am trying to make a player using a Class as a setup. These are my files:

Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <iostream>
#include <string>

using namespace std;

class Player
{
public:
	Player();
	void initPlayer(string name, int level, int maxExp);
	void displayPlayer();
private:
	string _name;
	int _level;
	int _exp;
	int _maxExp;
};


Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Player.h"

Player::Player()
{

}

void Player::initPlayer(string name, int level, int maxExp)
{
	_name = name;
	_level = level;
	_maxExp = maxExp;
}

void Player::displayPlayer()
{
	cout << "NAME: " << _name << " LEVEL: " << _level << endl;
	cout << "EXP: " << _exp << "/" << _maxExp << endl;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

#include "Player.h"

using namespace std;

void initPlayer(Player player);

int main()
{
	Player player;
	initPlayer(player);
	player.displayPlayer();
	system("PAUSE");
	return 0;
}

void initPlayer(Player player)
{
	player.initPlayer("Test", 1, 20);
}


The problem is, when I run player.displayPlayer(); it comes back without a name, and all of the int's are -858993460.

Anyone know what I am doing wrong?
The Player object passed to initPlayer is passed by value, meaning the function receives a copy of the Player object fed to it and any changes made to that copy are not reflected in the original.
void initPlayer(Player& player) // pass by reference
Thank you. That was the problem!
Topic archived. No new replies allowed.