编译器构造-在编译c++控制台我的程序时,我在包含的矢量头文件中得到错误,这是可能的吗?
compiler construction - in compiling c++ console my program, i get an error in the included vector header file, is it possible?
这是完整的错误信息
> In file included from
> c:mingwbin../lib/gcc/mingw32/4.7.2/include/c++/vector:63:0,
> from PlayerDatabase.h:4,
> from PlayerDatabase.cpp:6:
c:mingwbin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:
> In function 'void std::_Construct(_T1*, const _T2&)':
> c:mingwbin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:85:13:
> error: expected type-specifier before 'static_cast'
> c:mingwbin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:85:13:
> error: expected ')' before 'static_cast'
> c:mingwbin../lib/gcc/mingw32/4.7.2/include/c++/bits/stl_construct.h:85:13:
> error: expected ';' before 'static_cast'
包含在PlayerDatabase.h:4的文件是矢量头文件
我在我的cpp文件中使用了动态强制转换,将投手和击球手指针更改为基类棒球手指针,然后通过push_back()将它们插入向量,这是问题吗?
是playerdatabase.h文件
#include <vector>
#include "BaseballPlayer.h"
#include "Hitter.h"
#include "Pitcher.h"
//这是playerdatabase类的定义
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;
};
这是playerdatabase.cpp
#include <iomanip>
#include "cppMemDbg.h"
#include "PlayerDatabase.h"
const char HITTER='H';
const char PITCHER='P';
const int REDUCE_BY=4;
//不带参数的默认构造函数
PlayerDatabase::PlayerDatabase(){
}
//复制构造函数
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++;
}
}
//赋值操作
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;
}
//构造函数
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++;
}
}
}
//一个基于goodHitter()和goodPitcher()函数通过消除一些人来创建优秀团队的函数
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&);
我在你的整个代码中没有看到一个const
限定符,所以你可能没有意识到常量正确性。您应该对这个主题做一些研究,但它基本上表示操作是否会修改给定的参数。
相关文章:
- C++系统找不到指定的文件错误
- C++语法头文件错误
- C++生成文件错误"implicit entry/start for main executable"
- C++/生成文件错误:未定义对"main"的引用
- VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- C++程序不制作文件(错误)
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- 如何修复使用VScode调试器gcc调试时的"找不到文件"错误
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- Qt .exe文件错误
- c++ 添加具有写入文件错误的文本变量
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 写入文本文件错误,QT
- C++写入文件错误
- Windows Subsystem for Linux (WSL) 下的间歇性随机"找不到文件"错误
- 犰狳读取MAT文件错误
- 无法求解FullPathForFilename:COCOS2DX中可能缺少文件错误
- C Wininet FTP列出了许多文件错误
- 生成文件错误 - 找不到文件 - *.cpp