错误:调用terminate引发exceptionAbort陷阱:6

Error: terminate called throwing an exceptionAbort trap: 6

本文关键字:陷阱 exceptionAbort terminate 调用 错误 引发      更新时间:2023-10-16

我正在制作一个关于继承的烫手山芋游戏。我有马铃薯班、球员班和裁判班。

在我的裁判级别内调用投掷功能时,我一直出现同样的错误,我似乎不明白为什么:终止被调用的投掷异常中止陷阱:6

我仔细检查了一下,发现在裁判::start()中,在我的for循环中对Players.at(randU)->throw(*m)的调用导致了这个错误。但我觉得一切都很好。

有人能帮我吗?谢谢

裁判cc

#include <iostream>
#include <string>
#include "potato.h"
#include "player.h"
#include "umpire.h"
#include "PRNG.h"
using namespace std;
// Declare globally
int gamecount=1;
// GLOBAL VARIABLES
bool first=false; // False if set has not started
PRNG rplayer;
// UMPIRE CONSTRUCTOR-------------------------------------------------------------------
Umpire::Umpire( Player::PlayerList &players ){
Players=players;
cout<<"tMashed POTATO will go off after ";
cout.flush();
m=new Mashed(Players.size()-1);
cout << " tosses" << endl;
cout.flush();
cout <<"tFried POTATO will go off after 5 tosses" << endl;  
cout.flush();
f=new Fried(5);}
// UMPIRE DESTRUCTOR-------------------------------------------------------------------
Umpire::~Umpire(){  
delete m;
delete f;}

// UMPIRE START------------------------------------------------------------------------
void Umpire::start(){
int randU;
int gameCount=1; // Keeps track of sets
// Check if you are at the end of the list.
if(Players.size()==1){
// Who won?
cout << Players.at(0)->getId() << "wins the Match!" << endl;
cout.flush();}
else{
// Print output for sets----------------------------------------------------------------
// See which potato is being used in the set
if(gameCount%2!=0){ 
cout << "Set " << gameCount << "-tUser (mashed) [";
cout.flush();}
else{ 
cout << "Set " << gameCount << "-tUser (fried) [";
cout.flush();}
gameCount++; // increase gamecount
// Outputting players left in the set
for(unsigned int i=0;i<Players.size();i++){
cout<<Players.at(i)->getId();}
cout <<"]: ";
cout.flush();
//Start Tossing--------------------------------------------------------------------------
randU=rplayer(Players.size()-1);
// Output A(id) or R(id)
if (randU%2==0){
cout<<"A("<<randU<<"), ";
cout.flush();}
else{
cout<<"R("<<randU<<"), ";
cout.flush();}
if(first==false){
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==Players.at(randU)->toss(*m)){
Players.erase(Players.begin()+i);
cout << "Eliminated: "<< i << endl;
cout.flush();}
}
first=true;
f->reset();
start();
}
else{
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==Players.at(randU)->toss(*f)){
Players.erase(Players.begin()+i);
cout << "Eliminated: "<< i << endl;
cout.flush();}
} 
first=false;
m->reset();
start();
}
}
}

播放器.cc

#include <iostream>
#include "player.h"
#include "potato.h"
#include "PRNG.h"
using namespace std;
// GLOBAL DECLARATIONS--------------------------------------------------------------------
PRNG randP;
// PLAYER CONSTRUCTOR---------------------------------------------------------------------
Player::Player(unsigned int id, Player::PlayerList &players){
pid=id;
Players=players;
lrpFLAG=false;}
// getId() returns the player's id--------------------------------------------------------
unsigned int Player::getId(){
return pid;}
// RNPlayer Constructor-------------------------------------------------------------------
RNPlayer::RNPlayer( unsigned int id, Player::PlayerList &players ) : Player(id,players){}
// TOSS FUNCTION--------------------------------------------------------------------------
unsigned int RNPlayer::toss( Potato &potato ){
unsigned int randnum;
if(potato.countdown()){ return getId(); }
for(;;){    
randnum=randP(Players.size()-1);
if (randnum%2==0){
cout<<"A("<<randnum<<"), ";
cout.flush();}
else{
cout<<"R("<<randnum<<"), ";
cout.flush();}
// If our randomly selected player is not the current player...
if(Players.at(randnum)->getId()!=getId()){
break;}
}   
return Players.at(randnum)->toss(potato);
}
// LRPlayer Constructor-------------------------------------------------------------------
LRPlayer::LRPlayer( unsigned int id, Player::PlayerList &players ) : Player(id,players){}
// TOSS FUNCTION
unsigned int LRPlayer::toss( Potato &potato ){
unsigned int current; // current player
// Find who our current player is
for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==getId()){
current=i;
cout<<"A("<<i<<"), ";}
}
// if timer hasn't gone off yet...
if(potato.countdown()!=true){
// if this is the FIRST toss, we want to toss left
if(lrpFLAG==false){
if(current==0){
lrpFLAG=true;
(Players.at(Players.size()-1))->toss(potato);}
else{
lrpFLAG=true;
(Players.at(current-1))->toss(potato);}
}
else{
if(current==Players.size()-1){
lrpFLAG=false;
(Players.at(0))->toss(potato);}
else{
lrpFLAG=false;
(Players.at(current+1))->toss(potato);}
}
}
return (Players.at(current))->getId();
}

Main.cc

#include <iostream>
#include <vector>
#include <time.h>
#include "potato.h"
#include "player.h"
#include "umpire.h"
#include "PRNG.h"
using namespace std;
int main(int argc, char *argv[] ){
int p = 5;
int tmp;
unsigned int s;
PRNG prng1;
prng1.seed(1234567890);
// Parse the command line arguments
switch ( argc ) {
case 3:
tmp=atoi(argv[1]);
s=atoi(argv[2]);
prng1.seed(s);
if(tmp<2 || tmp>20){
cout << "Player must be between 2 and 20 inclusive" << endl;
return 0;}
else{
p = atoi(argv[1]);}
}
// Creating list of players.
Player::PlayerList players;
for(int i=0; i<p; i++){
if(i%2==0){
players.push_back(new LRPlayer(i,players));}
else{               
players.push_back(new RNPlayer(i,players));}
}
//for (int i=0;i<players.size();i++){
//  cout << "Player at " << i << " id: " << players.at(i)->getId() << endl;}
// How many players?----------------------------------------------------------------------
cout << p << " players in the match" << endl;
// Construct an UMPIRE--------------------------------------------------------------------
Umpire u(players);
// Start the game!------------------------------------------------------------------------
u.start();
}

还要注意:PRNG.h是一个生成随机数的类。所以PRNG prng1;int rand=prng1(#)生成一个介于0和#之间的随机数。

另外:当我调用throw函数时,问题就出现了。我没有超出范围,因为当我尝试调用Players.at(randU)->getID()时,我根本没有得到任何错误。可能是我无法达到那个玩家的投掷功能吗?我想这和我所指的或记忆有关。我首先在我的main.cc中制作了一个PlayerList播放器,并在两者之间交替使用push_back播放器。但每个玩家也会收到一份名单。也许我遇到了与此相关的错误?

谢谢!非常感谢您的帮助:)

抛出的异常必须是std::out_of_range。生成的随机数必须大于矢量中的元素数。

调用Players.erase()后,必须重新生成

randU。否则它将超出范围
或者你应该打破循环为:

for(unsigned int i=0; i<Players.size(); i++){
if(Players.at(i)->getId()==Players.at(randU)->toss(*f)){
Players.erase(Players.begin()+i);
cout << "Eliminated: "<< i << endl;
cout.flush();
break; // BREAK HERE as randU now can be greater than (Players.size() - 1)
}
}