C++ 在编译c++;控制台我的程序,我在包含的向量头文件中得到一个错误,可能吗?
这是完整的错误消息C++ 在编译c++;控制台我的程序,我在包含的向量头文件中得到一个错误,可能吗?,c++,compiler-construction,vector,warnings,C++,Compiler Construction,Vector,Warnings,这是完整的错误消息 > 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/i
> 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'
PlayerDatabase.h:4中包含的文件是向量头文件
我在我的cpp文件中使用了动态强制转换,将投手和击球手指针更改为基类baseballplayer指针,然后通过push_baxk()将它们插入向量,这是问题吗
这是playerdatabase.h文件
#include <vector>
#include "BaseballPlayer.h"
#include "Hitter.h"
#include "Pitcher.h"
}
//复制构造函数
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++;
}
}
这部分有什么问题吗
std::vector<BaseballPlayer*>::iterator begin, end;
begin=right.team.begin();
end=right.team.end();
std::vector::迭代器开始,结束;
begin=right.team.begin();
end=right.team.end();
代码太多了!但有几件事是错误的:
PlayerDatabase(PlayerDatabase&); // this is the one the error is complaining about
这声明了一个可变副本构造函数。如果你想要的话没关系,但你可能不想要。您希望这样做:
PlayerDatabase(PlayerDatabase const&);
对于赋值运算符也是一样,除非它需要修改源(而您不想这样做),否则它应该是:
PlayerDatabase& operator= (PlayerDatabase const&);
我在您的整个代码中没有看到一个
const
限定符,因此您可能不知道const的正确性。你应该对这个主题做一些研究,但它基本上是说这个操作是否会修改给定的参数。你也可以显示你的代码吗?你不可能在你的CPP文件中有一个\define new DEBUG\u new
?对于那些感兴趣的人,我的第85行是::new(static\u cast(\u p))\u T1(\u value)
您是否在vector
之前包含的任何头文件中定义了new
其他内容?很可能您在之前包含的“cppmendbg.h”
中出错了
谢谢,我在声明“PlayerDatabase(const PlayerDatabase&)”时总是出错
PlayerDatabase::~PlayerDatabase(){
std::vector<BaseballPlayer*>::iterator iter;
for (iter=team.begin();iter!=team.end();iter++){
if (*iter)
delete *iter;
*iter=nullptr;
}
team.clear();
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;
}
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++;
}
}
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;
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();
}
int PlayerDatabase::get_team_count(){
return team.size();
std::vector<BaseballPlayer*>::iterator begin, end;
begin=right.team.begin();
end=right.team.end();
PlayerDatabase(PlayerDatabase&); // this is the one the error is complaining about
PlayerDatabase(PlayerDatabase const&);
PlayerDatabase& operator= (PlayerDatabase const&);