C++ 错误:调用terminate时抛出exceptionAbort陷阱:6

C++ 错误:调用terminate时抛出exceptionAbort陷阱:6,c++,threadabortexception,C++,Threadabortexception,我正在制作一个关于遗产的烫手山芋游戏。我有土豆课、球员课和裁判课 在我的裁判员类中调用toss函数时,我一直遇到同样的错误,我似乎不明白为什么:terminate调用抛出异常bort trap:6 我检查了一下,发现在裁判员::start()中,在for循环中对Players.at(randU)->toss(*m)的调用导致了这个错误。但我觉得一切都很好 有人能帮我吗?谢谢 裁判员。cc #include <iostream> #include <string> #inc

我正在制作一个关于遗产的烫手山芋游戏。我有土豆课、球员课和裁判课

在我的裁判员类中调用toss函数时,我一直遇到同样的错误,我似乎不明白为什么:terminate调用抛出异常bort trap:6

我检查了一下,发现在裁判员::start()中,在for循环中对Players.at(randU)->toss(*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();
        }
    }
}
#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();
}
#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();

}
#包括
#包括
#包括“potato.h”
#包括“player.h”
#包括“裁判员h”
#包括“PRNG.h”
使用名称空间std;
//全球申报
int gamecount=1;
//全局变量
bool first=false;//如果设置尚未启动,则为False
PRNG-rplayer;
//裁判员-------------------------------------------------------------------
裁判员:裁判员(球员:球员名单和球员){
玩家=玩家;

cout引发的异常必须是std::out\u超出范围。您正在生成的随机数必须高于向量中的元素数。

randU
必须在调用
Players.erase()
后重新生成。否则它将超出范围。
或者,您应该打破循环,如下所示:

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)
            }
        } 
for(unsigned int i=0;igetId()==Players.at(randU)->toss(*f)){
Players.erase(Players.begin()+i);

请阅读。同时在调试器中运行您的程序,它将帮助您找到引发异常的位置,并让您检查变量,以了解发生异常的原因。对于初学者,请查看异常是什么。但这几乎肯定是一个超出范围的错误。因此,请将向量大小和参数的计算结果打印到
at()
并验证是否在预期范围内。如何确定异常是什么?通常您会使用
尝试{code\u throwing\u goes\u here()}catch(std::exception const&e){std::cout randU在擦除后重新生成,因为我在start()上递归再次。但是,无论如何都不会为我删除收缩向量?因此,当我调用randU时,我不会再次遇到该问题?
randU
for(unsigned int i=0;看这是个问题,谢谢!但是我仍然遇到调用toss本身的问题…请详细说明
toss()
调用问题。如果您可以减少代码大小或将整个源代码作为存档文件下载,也会有所帮助。