Need some help here it seems to compile fine but crashes at runtime. Am I missing something here?

#include <iostream>
#include <string>
using namespace std;

// player class
class Player{
	int *position;
public:
	Player();
	Player(int);
	~Player();
	void print_pos(){
		cout << "player 1 is in position: " << *position << endl;
	}
};

Player::Player(){ // main constructor
	position = new int;
	*position = 0;
}
Player::Player(int pos){  //overloaded constructor
	*position = pos;
}
Player::~Player(){ // destructor
	delete position;
}


int main()
{
	Player one;
	Player two(1);
	one.print_pos();
	two.print_pos();
	return 0;
}

Look at your "overloaded" constructor. Does it allocate memory? Does it dereference the pointer as if memory were allocated?

commented: excellent +3

Thank you very much I'm learning very fast why c++ guru's are so precise about every minor detail.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.