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* â
Here is the line of code:
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?
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*;
// first one is a pitcher
team = new Pitcher();
// second one is a hitter
team = new Hitter();
// then to clean up:
The functions I'm using are:
void PlayerDatabase::load_team(ifstream& infile)
/*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;