Hi from what I have seen this should be correct but compiler is complaining.

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

// player class
class Player{
	int *position;
	string name;

public:
	Player();
	~Player();
	void set_name(){
		cout << "enter name: ";
		getline(cin,name);
		cout << "name is now: " << name << endl;
	}
	void print_pos(){
		cout << name << " is in position: " << *position << endl;
	}
	void set_pos(int pos){
		*position = pos;
	}
};

Player::Player(){ // main constructor
	position = new int;
	*position = 0;
	set_name();
}


Player::~Player(){ // destructor
	delete position;
}


int main()
{

	int num_players = 0;
	int exit = 9;
	while(exit != 9){
		cout << "1. Create player\n9. Exit\n";
		cout << "Choice: ";
		cin >> exit;

		if(exit == 1){
			num_players += 1;
			Player* player = new Player[num_players];
		}
	delete[] player;
	}
	return 0;
}
error C2065: 'players' : undeclared identifier
error C2541: 'delete' : cannot delete objects that are not pointers

Thank you greatly for your time.

line 52: where is player declared? The declaration on line 50 is not in scope at line 52, so it can't be used. Move the declaration of player from line 50 to just after line 42.

why is position a pointer? line 27 only allocates one of them. There is no point using a pointer to allocate a single integer.

commented: gettin me straightened up slowly but surely lol +3

Yeah position doesn't really need to be a pointer I'm just playing around with a few concepts as I am new to c++. I see what you are saying about it not being in scope but I do want it to add new players at that position in the code so I would declare at line 43 and then leave my new statement in the same location?

Correct. An even better method would be to use the std::vector container. But you may not be ready for that yet.

Ok I changed up the pointer to the array of objects but now at runtime it seems to be making calls to set_name() for every new element in the array. I was expecting it would only call it once per creation of the object any ideas on that? Here is the new code.

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

// player class
class Player{
	int *position;
	string name;

public:
	Player();
	~Player();
	void set_name(){
		cout << "enter name: ";
		getline(cin,name);
		cout << "name is now: " << name << endl;
	}
	void print_pos(){
		cout << name << " is in position: " << *position << endl;
	}
	void set_pos(int pos){
		*position = pos;
	}
};

Player::Player(){ // main constructor
	position = new int;
	*position = 0;
	set_name();
}


Player::~Player(){ // destructor
	delete position;
}


int main()
{

	int num_players = 0;
	int exit = 0;
	Player* player;
	do{
		cout << "1. Create player\n9. Exit\n";
		cout << "Choice: ";
		cin >> exit;

		if(exit == 1){
			num_players += 1;
			player = new Player[num_players];
		}
	}while(exit != 9);
	delete[] player;
	return 0;
}

Look at the logic of the code. Every time you select menu 1 the program allocates an entirely new array of Player objects. The previous array is thrown away, lost forever, and a memory leak occurs. If an array already exists then you need to figure out how to reallocate the array without losing any memory or the previous values in the array. This is one reason why programmers use the std::vector container, because it does all that for you so you can concentrate on the logic of your program instead of how to manage arrays.

And yes, the constructor has to be called for each item in the array, once for value of num_players.

Ok well maybe I will fool around with some simpilar arrays to learn even more about them, they are a bit more in depth then I would have first imagined. I should be able to figure out what's wrong with what you have givin me. I suppose after some tinkering around with this I'll have a good look at std::vector. Thank you so much for your time and patience ancient.

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.