Hey everyone, I'm all out of idea on what I am doing wrong here. I'm trying to overload the '=' operator by dynamically allocating a new array of pointers to class objects. Here is the error message:
PlayerDatabase.cc: In member function âPlayerDatabase& PlayerDatabase::operator=(const PlayerDatabase&)â:
PlayerDatabase.cc:173: error: incompatible types in assignment of âBaseballPlayer**â to âBaseballPlayer* [100]â
Here is the line of code:
//line 173
team = new BaseballPlayer* [team_count];
I am making a player database and 'team' is the name of the array to hold each player. BaseballPlayer is a base class w/ Pitcher and Hitter derived class. Any help would be greatly appreciated. Thank everyone.
It is like the one above, but even with changing it, I get the same error. Also if I change it to the way you suggested, I'll have to change all the functions that use that array as well right? I'll do some more reading...
It is like the one above, but even with changing it, I get the same error.
Well then you'll have to show me more code.
Also if I change it to the way you suggested, I'll have to change all the functions that use that array as well right?
Yep.
I'll do some more reading...
Well.. okay. But I don't know if you'll find anything useful.
Since you have BaseballPlayer* team;, this indicates you have one of two possible situations:
1) a pointer to a single BaseballPlayer (or any derived) type
2) a pointer to an array of BaseballPlayer (but not derived) types.
What you want is a pointer to an array of BaseballPlayer or derived types. So you need the **.
You'll also need to allocate each team member individually and assign them to the array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 5 players on this team
BaseballPlayer** team = new BaseballPlayer*[5];
// first one is a pitcher
team[0] = new Pitcher();
// second one is a hitter
team[1] = new Hitter();
// etc
// then to clean up:
delete team[0];
delete team[1];
delete[] team;
int team_count;
BaseballPlayer* team[100];
int totalEarnedRuns;
float totalInnings;
int totalAtBats;
int totalHits;
};
#endif
The functions I'm using are:
void PlayerDatabase::load_team(ifstream& infile)
{
char playerType;
/*while the end of the file is not reached, test to see if the next
player is a hitter or pitcher, then input the data into the
appropriate fields by using the derived class's load_player function,
keep a tally of the total atbats, hits, earned runs, and innings
pitched and increment the team_count
*/
infile >> playerType;