Dynamically allocate an array of Pointers to class objects

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.

-Pat
I'm guessing team is defined like so:

 
BaseballPlayer* team;


If so, this is wrong. It should be like this:

 
BaseballPlayer** team;
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...

-P
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;
Last edited on
#include "BaseballPlayer.h"
#include "Pitcher.h"
#include "Hitter.h"

#ifndef PLAYERDATABASE_H
#define PLAYERDATABASE_H
using namespace std;


class PlayerDatabase
{
public:
~PlayerDatabase();
void print_team() const;
PlayerDatabase& operator=(const PlayerDatabase&);
PlayerDatabase(const PlayerDatabase&);
PlayerDatabase();
void load_team(ifstream&);
void print_team();
virtual void get_inningsPitched(){};
private:

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;

while(!infile.eof())
{
if(playerType == 'P')
{
team[team_count] = new Pitcher();
team[team_count]->load_player(infile);
totalInnings += team[team_count]->get_inningsPitched();
totalEarnedRuns += team[team_count]->get_earnedRuns();
team_count++;
}
else
{
team[team_count] = new Hitter();
team[team_count]->load_player(infile);
totalAtBats += team[team_count]->get_atbats();
totalHits += team[team_count]->get_hits();
team_count++;
}
infile >> playerType;
}
}
oh okay...

BaseballPlayer* team[100];

So it was neither of the ones I guessed.

This will work, but you're limited to no more thann 100 players on the team.

team = new BaseballPlayer* [team_count];

No need to do this, because you already have an array of 100 team members.
That did it thanks for the help Disch
Topic archived. No new replies allowed.