Я работаю над заданием, в котором нас просят смоделировать процедуру «отборочного голосования» для реальной игры.Для этого мы используем один класс (Vote
) для записи имени игрока, который будет удален, и другой (Voting
), который нигде не создается, потому что его члены статичны (нам нужно, чтобы они были).Голоса хранятся в векторе внутри класса Voting.
Проблема в том, что даже когда я использую функцию push_back
для сохранения голосов в векторе, после запуска программы в течение нескольких раз я получаюstd::length_error
с what(): basic_string::_M_create
.После того, как я закрою его и перезапущу, в первый раз я получаю сообщение об ошибке, похожее на заголовок (std::bad_alloc
).
Есть идеи?Я подозреваю, что это связано с тем, что вектор неправильно освобождает память, но я не уверен.
Мой код ( Voting.h ):
#ifndef VOTING_H_INCLUDED
#define VOTING_H_INCLUDED
#include <vector>
#include <map>
#include "Vote.h" //ignore this one
#include "Team.h" //this one too
class Voting
{
public:
static vector <Vote> votes; //votes cast during voting procedure
static map <string, int> results; //voting results (key: name, value: number of votes)
static void votingProcess(Team &team); //the whole voting procedure
};
#endif // VOTING_H_INCLUDED
Voting.cpp :
#include <vector>
#include <map>
#include <cstdlib>
#include "Vote.h"
#include "Voting.h"
using namespace std;
vector <Vote> Voting::votes(1); //initialize the vector for the votes
void voteCast(Team &team);
void Voting::votingProcess(Team &team)
{
voteCast(team);
}
void voteCast(Team &team)
{
int playerNumber = team.getNumberOfPlayers(); //number of players right now still in the team
int votingPlayer = 0; //index of the currently voting player
int votedPlayerIndex = -1; //index of the player to be voted for elimination
int reasonIndex = -1; //index of the selected reason for voting
Vote draft; //temporary empty vote
srand(time(NULL)); // initialize the RNG
Voting::votes.clear(); //clear the vector of any past votes
string reasons[4] = {"Den ta pame kala metaxi mas", "Einai ikanoteros/i apo emena kai ton/ti theoro apeili", "Den exei na prosferei kati stin omada", "Prospathei sinexeia na sampotarei tis prospatheies mas"};
//some reasons for elimination (i've tried smaller strings and even chars and it didn't work, so don't bother)
do
{
if (team.getPlayers()[votingPlayer].getAge() != 0 && team.getPlayers()[votingPlayer].getVotes() > 0)
//if the player with index votingPlayer has not been eliminated and has still votes to cast
{
do
{
votedPlayerIndex = rand() % 11; //select a random player for elimination
reasonIndex = rand() % 5; //select a random reason for it
}
while ((team.getPlayers()[votedPlayerIndex].getAge() == 0) || (votedPlayerIndex == votingPlayer) || (team.getPlayers()[votedPlayerIndex].getImmunity() == true));
// selection continues if the selected player has already been eliminated,
//if the selected player is the same as the voting player or if the selected player has immunity from elimination
team.getPlayers()[votingPlayer].setVotes(team.getPlayers()[votingPlayer].getVotes() - 1); //reduce the player's available votes by 1
draft.setVotedPlayer(team.getPlayers()[votedPlayerIndex].getName()); //write the name of the player to be voted in an empty Vote object
draft.setReason(reasons[reasonIndex]); //and the reason too
Voting::votes.push_back(draft); //push the Vote obj. in the vector
}
else
{
votingPlayer++; //ignore and get to the next player
}
}
while (votingPlayer < playerNumber); //vote casting continues until every player has casted a vote
}
Vote.h :
#ifndef VOTE_H_INCLUDED
#define VOTE_H_INCLUDED
#include <string>
#include <iostream>
using namespace std;
class Vote
{
string voted; //name of the player to be eliminated
string reason; //reason for elimination
public:
Vote() { voted = ""; reason = ""; } //constructor without parameters
Vote(string player, string reason) { voted = player; this -> reason = reason;} //constructor with parameters (this one is used)
~Vote() { cout << "Vote object destroyed" << endl; }; //destructor
string getVotedPlayer() { return voted; } //getters
string getReason() { return reason; }
void setVotedPlayer(string player) { voted = player; } //setters
void setReason(string reason) { this -> reason = reason; }
void status() { cout << "Voted player: " << voted << endl << "Reason: " << reason << endl;} //status function
};
#endif // VOTE_H_INCLUDED