c++vectorwarnings

in compiling c++ console my program, i get an error in the included vector header file, is it possible?


this is the full error message

> In file included from
> c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/vector:63:0,
>                      from PlayerDatabase.h:4,
>                      from PlayerDatabase.cpp:6: 

c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:
> In function 'void std::_Construct(_T1*, const _T2&)':

> c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:85:13:
> error: expected type-specifier before 'static_cast'

> c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:85:13:
> error: expected ')' before 'static_cast'

> c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:85:13:
> error: expected ';' before 'static_cast' 

the file included at PlayerDatabase.h:4 is the vector header file

I have used a dynamic cast in my cpp file, to change pitcher and hitter pointers to a base class baseballplayer pointers before inserting them through push_baxk() into the vector, is that the problem?

this is the playerdatabase.h file

#include <vector>
#include "BaseballPlayer.h"
#include "Hitter.h"
#include "Pitcher.h"

// this is the playerdatabase class definition

class PlayerDatabase {

public:
    PlayerDatabase();
    PlayerDatabase(PlayerDatabase&);
    PlayerDatabase& operator= (PlayerDatabase&);
    ~PlayerDatabase();
    void print_team(std::ofstream&);
    void load_team(std::ifstream&);
    void create_good_team();
    void create_small_team();
    int get_team_count();

private:
    std::vector<BaseballPlayer*> team;
};

and this is the playerdatabase.cpp

#include <iomanip>
#include "cppMemDbg.h"
#include "PlayerDatabase.h"

const char HITTER='H';
const char PITCHER='P';
const int REDUCE_BY=4;

// the default constructor without arguments

PlayerDatabase::PlayerDatabase(){

}

// the copy constructor

PlayerDatabase::PlayerDatabase(PlayerDatabase& right){
std::vector<BaseballPlayer*>::iterator begin, end;
begin=right.team.begin();
end=right.team.end();

while (begin!=end){
    Pitcher* pPlayer= dynamic_cast<Pitcher*>(*begin);
    if (pPlayer){
        Pitcher* pitcher= new Pitcher(*pPlayer);
        team.push_back(dynamic_cast<BaseballPlayer*>(pitcher));
    }

    else{
        Hitter* hPlayer= dynamic_cast<Hitter*>(*begin);
        Hitter* hitter= new Hitter(*hPlayer);
        team.push_back(dynamic_cast<BaseballPlayer*>(hitter));
    }

    begin++;
}

}

// the assignment operator

PlayerDatabase& PlayerDatabase::operator= (PlayerDatabase& right){
if (team==right.team)
    return *this;

else {
    std::vector<BaseballPlayer*>::iterator begin, end;
    begin=right.team.begin();
    end=right.team.end();

    while (begin!=end){

        Pitcher* pPlayer= dynamic_cast<Pitcher*>(*begin);
        if (pPlayer){
            Pitcher* pitcher= new Pitcher(*pPlayer);
            BaseballPlayer* bplayer= dynamic_cast<BaseballPlayer*>(pitcher);
            team.push_back(bplayer);
        }

        else{
            Hitter* hPlayer= dynamic_cast<Hitter*>(*begin);
            Hitter* hitter= new Hitter(*hPlayer);
            BaseballPlayer* bplayer= dynamic_cast<BaseballPlayer*>(hitter);
            team.push_back(bplayer);
        }
        begin++;
    }
}
return *this;

}

// the destructor

PlayerDatabase::~PlayerDatabase(){
std::vector<BaseballPlayer*>::iterator iter;
for (iter=team.begin();iter!=team.end();iter++){
    if (*iter)
        delete *iter;
    *iter=nullptr;
}
team.clear();

}

// a function that prints the baseball players onto the stdout and writes them into the given file

void PlayerDatabase::print_team(std::ofstream& outputFile){
int totalHits=0, totalAtBats=0, totalEarnedRuns=0;
float totalInningsPitched=0.0;

std::vector<BaseballPlayer*>::iterator begin, end;
begin=team.begin();
end=team.end();
int member=1;

while (begin!=end){
    std::cout<<"Member "<<member<<std::endl;
    (*begin)->print_player(outputFile);

    Pitcher* pPlayer= dynamic_cast<Pitcher*>(*begin);
    if (pPlayer){
        totalEarnedRuns+=pPlayer->earnedRuns;
        totalInningsPitched+=pPlayer->inningsPitched;
        begin++;
        member++;
    }

    else{
        Hitter* hPlayer= dynamic_cast<Hitter*>(*begin);
        totalHits+=hPlayer->hits;
        totalAtBats+=hPlayer->atBats;
        begin++;
        member++;
    }

}

if(totalAtBats==0||totalInningsPitched==0.0){

    if(totalAtBats==0&&totalInningsPitched==0.0){
        std::cout<<"Team Batting Average: "<<"n/a"<<std::endl;
        std::cout<<"Team ERA: "<<"n/a"<<std::endl;
    }
    else if (totalAtBats==0){
        std::cout<<"Team Batting Average: "<<"n/a"<<std::endl;
        std::cout<<"Team ERA: "<<(totalEarnedRuns/totalInningsPitched*9)<<std::endl;
    }
    else {
        std::cout<<"Team Batting Average: "<<std::fixed<<std::setprecision(3)<<totalHits/(double)totalAtBats<<std::endl;
        std::cout<<"Team ERA: "<<"n/a"<<std::endl;
    }
}

else{
    std::cout<<"Team Batting Average: "<<std::fixed<<std::setprecision(3)<<totalHits/(double)totalAtBats<<std::endl;
    std::cout<<"Team ERA: "<<(totalEarnedRuns/totalInningsPitched*9)<<std::endl;
}

}

// loads baseball players from an input file that is formatted

void PlayerDatabase::load_team(std::ifstream& input){
int counter=1;
while (!input.eof()){
    char playerType;
    input.get(playerType);

    if (playerType==HITTER){
        Hitter* hitter= new Hitter;
        hitter->load_player(input);
        std::cout<<"Loading member "<<counter<<std::endl;
        team.push_back(dynamic_cast<BaseballPlayer*>(hitter));
        counter++;
    }

    else if (playerType==PITCHER){
        Pitcher* pitcher= new Pitcher;
        pitcher->load_player(input);
        std::cout<<"Loading member "<<counter<<std::endl;
        team.push_back(dynamic_cast<BaseballPlayer*>(pitcher));
        counter++;
    }

}

}

// a function to create a good team by eliminating some based on the goodHitter() and goodPitcher() functions

void PlayerDatabase::create_good_team(){

std::vector<BaseballPlayer*> goodTeam;

std::vector<BaseballPlayer*>::iterator begin, end;
begin=team.begin();
end=team.end();

while (begin!=end){

    Pitcher* pPlayer= dynamic_cast<Pitcher*>(*begin);
    if (pPlayer){
        if(pPlayer->goodPitcher()){

            goodTeam.push_back(dynamic_cast<BaseballPlayer*>(pPlayer));
        }
        else {
            if (*begin)
                delete *begin;
            *begin=nullptr;
        }
        begin++;
    }

    else{
        Hitter* hPlayer= dynamic_cast<Hitter*>(*begin);
        if(hPlayer->goodHitter()){
            goodTeam.push_back(dynamic_cast<BaseballPlayer*>(hPlayer));
        }
        else {
            if (*begin)
                delete *begin;
            *begin=nullptr;
        }
        begin++;
    }
}
team.clear();
team=goodTeam;

}

// creates a small team deleting the first four players in the vector

void PlayerDatabase::create_small_team(){
if(get_team_count()>=REDUCE_BY){
    std::vector<BaseballPlayer*>::iterator first, fifth;
    first= team.begin();
    fifth=first+REDUCE_BY;

    while (first!=fifth){
        if (*first)
            delete *first;
        *first=nullptr;
        first++;
    }
    team.erase(team.begin(),fifth);
}

else {
    std::vector<BaseballPlayer*>::iterator first,last;
    first= team.begin();
    last=team.end();
    while(first!=last){
        if (*first)
            delete *first;
        *first=nullptr;
        first++;
    }
    team.clear();
}

} // number of team members

int PlayerDatabase::get_team_count(){
return team.size();

}

is there something wrong with this part?

 std::vector<BaseballPlayer*>::iterator begin, end;
 begin=right.team.begin();
 end=right.team.end();

Solution

  • That's a lot of code! But a few things that are lets say wrong with it:

    PlayerDatabase(PlayerDatabase&); // this is the one the error is complaining about
    

    This declares a mutable copy-constructor. It's fine if you want that, but you probably don't. You want this instead:

    PlayerDatabase(PlayerDatabase const&);
    

    Same thing for your assignment operator, unless it needs to modify the source (and you don't wanna do that), it should be:

    PlayerDatabase& operator= (PlayerDatabase const&);
    

    I don't see a single const qualifier in your entire code, so you probably are not aware of const-correctness. You should do some research on the subject, but it basically says whether the operation will modify or not a given argument.