Here is a snippet of your code.
29 30 31 32 33 34 35 36 37 38 39 40 41
|
if (player[0]=="Avalanche") { //identify each player.
Avalanche player1;
} else if (player[0]=="Bureaucrat") {
Bureaucrat player1;
} else if (player[0]=="Toolbox") {
Toolbox player1;
} else if (player[0]=="Crescendo") {
Crescendo player1;
} else if (player[0]=="PaperDoll") {
PaperDoll player1;
} else if (player[0]=="FistfullODollars") {
FistfullODollars player1;
}
|
You have defined
player1
6 times, each time with a different type. And each time, as soon as player1 is defined, it goes out of scope and is destroyed. So, by the end of line 41, there is no longer a player1 variable defined.
You need to define player1 as a pointer to a base class, and in each of these if statements, create an object dynamically and assign it to the pointer.
29 30 31 32 33 34 35 36 37 38
|
PlayerBaseClass* player1;
if (player[0]=="Avalanche") { //identify each player.
player1 = new Avalanche;
} else if (player[0]=="Bureaucrat") {
player1 = new Bureaucrat;
}
// etc.
// at the end
delete player1;
|
Make sure you delete the pointers when you are done (or, better yet, use a smart pointer to do it for you).
Better yet, make an array of PlayerBaseClass*, and add a new function that does the common stuff.
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
|
PlayerBaseClass* populatePlayer(std::string typeString)
{
PlayerBaseClass* player;
if (typeString=="Avalanche") { //identify each player.
player = new Avalanche;
} else if (typeString=="Bureaucrat") {
player = new Bureaucrat;
}
// etc.
return player;
}
// Code from startGame
...
PlayerBaseClass* players[8];
for (int i = 0; i < 8; ++i)
{
players[i] = populatePlayer[i];
}
// when done
for (int i = 0; i < 8; ++i)
{
delete players[i];
players[i] = nullptr;
}
...
|
By the way, the code snippet you gave us uses an undefined variable
length
in line 21.