C++ C++;-矢量分割误差

C++ C++;-矢量分割误差,c++,linux,g++,ubuntu-13.10,blackjack,C++,Linux,G++,Ubuntu 13.10,Blackjack,我目前正在为我的大学课程做一个名为21点的CMSC项目。我正在尝试将一个名为Card的类添加到另一个名为Hand的类中的向量对象中。Hand类作为对象存储在名为Player的类中的另一个向量中 我的问题是,我试图在一个名为Blackjack的类中调用OutputPlayerHand方法,但我得到了一个分段错误 这是我的Blackjack.cpp类代码 #include "Blackjack.h" #include <iostream> Blackjack::Blackjack()

我目前正在为我的大学课程做一个名为21点的CMSC项目。我正在尝试将一个名为Card的类添加到另一个名为Hand的类中的向量对象中。Hand类作为对象存储在名为Player的类中的另一个向量中

我的问题是,我试图在一个名为Blackjack的类中调用OutputPlayerHand方法,但我得到了一个分段错误

这是我的Blackjack.cpp类代码

#include "Blackjack.h"
#include <iostream>

Blackjack::Blackjack()
{
  // Initialize the dealer
  Player dealer((char *) "Dealer", 100);
  m_dealer = dealer;

  // Initialize a player 'Jane' with 100 funds
  Player player((char *) "Jane", 100);
  m_players.push_back(player);
}

Blackjack::Blackjack(char *names[], int numPlayers)
{
  // Initialize the dealer
  Player dealer((char *) "Dealer", 100);
  m_dealer = dealer;

  // Loop through all passed player names
  for(int i = 0; i < numPlayers; i++)
    {
      // Initialize a player 'names[i]' with 100 funds
      Player player(names[i], 100);
      m_players.push_back(player);
    }
}

int Blackjack::GetNumPlayers()
{
  // Return the size of the players vector
  return m_players.size();
}

char *Blackjack::GetPlayerName(int player)
{
  // Return the requested player's name
  return m_players[player].GetName();
}

int Blackjack::GetPlayerFunds(int player)
{
  // Return the requested player's funds
  return m_players[player].GetFunds();
}

void Blackjack::SetPlayerFunds(int player, int amt)
{
  // Set the requested player's funds
  m_players[player].SetFunds(amt);
}

bool Blackjack::SetPlayerBet(int player, int amt)
{ 
  // If the player has insufficient funds
  if(m_players[player].GetFunds() < amt)
    {
      // Return false
      return false;
    }

  // Subtract the amount from the player funds
  m_players[player].SetFunds(m_players[player].GetFunds() - amt);
  // Add the amount to the player bet
  m_players[player].SetBet(amt);
  // Return true
  return true;
}

void Blackjack::NewDeal()
{
  // Create a new unsorted 52 card deck
  Deck deck;
  // Initialize m_deck to the new deck
  m_deck = deck;
  // Shuffle m_deck
  m_deck.Shuffle();

  // 2 cards for dealer, 2 cards for each player
  int cardsToDeal = 2 + (2 * m_players.size());

  // While we still have cards to deal
  while(cardsToDeal > 0)
    {
      // Deal to each player
      for(unsigned int i = 0; i < m_players.size(); i++)
    {
      std::cout << "Deal Player Card" << std::endl;
      // Deal one card to the player
      m_players[i].GetHand().AddCard(m_deck.DealCard());
      // Decrement the number of cards to deal
      cardsToDeal--;
    }

      std::cout << "Deal Dealer Card" << std::endl;
      // Deal the dealer one card
      m_dealer.GetHand().AddCard(m_deck.DealCard());
      // Decrement the number of cards to deal
      cardsToDeal--;
    }
}

void Blackjack::OutputPlayerHand(int player)
{
  std::cout << "Player Output Card." << std::endl;
  m_players[player].GetHand().GetCard(0).OutputCard();
}

void Blackjack::OutputDealerHand()
{
  // TODO: Code Method
}

bool Blackjack::HitPlayer(int player)
{
  // TODO: Code Method
  return false;
}

void Blackjack::DealerPlay()
{
  // TODO: Code Method
}

int Blackjack::SettlePlayerBet(int player)
{
  // TODO: Code Method
  return -1;
}
#include "Player.h"

Player::Player()
{
  m_name = (char *) "Jane";
  m_funds = 100;
  m_bet = 0;
}

Player::Player(char *name, int funds)
{
  m_name = name;
  m_funds = funds;
  m_bet = 0;
}

char *Player::GetName()
{
  return m_name;
}

void Player::SetName(char *name)
{
  m_name = name;
}

int Player::GetFunds()
{
  return m_funds;
}

void Player::SetFunds(int funds)
{
  m_funds = funds;
}

int Player::GetBet()
{
  return m_bet;
}

void Player::SetBet(int bet)
{
  m_bet = bet;
}

Hand Player::GetHand()
{
  return m_hand;
}
#include "Hand.h"

void Hand::AddCard(Card card)
{
  m_cards.push_back(card);
}

Card Hand::GetCard(int card)
{
  return m_cards[card];
}

int Hand::Size()
{
  return m_cards.size();
}

void Hand::Clear()
{
  m_cards.clear();
}
这是我的Hand.cpp类代码

#include "Blackjack.h"
#include <iostream>

Blackjack::Blackjack()
{
  // Initialize the dealer
  Player dealer((char *) "Dealer", 100);
  m_dealer = dealer;

  // Initialize a player 'Jane' with 100 funds
  Player player((char *) "Jane", 100);
  m_players.push_back(player);
}

Blackjack::Blackjack(char *names[], int numPlayers)
{
  // Initialize the dealer
  Player dealer((char *) "Dealer", 100);
  m_dealer = dealer;

  // Loop through all passed player names
  for(int i = 0; i < numPlayers; i++)
    {
      // Initialize a player 'names[i]' with 100 funds
      Player player(names[i], 100);
      m_players.push_back(player);
    }
}

int Blackjack::GetNumPlayers()
{
  // Return the size of the players vector
  return m_players.size();
}

char *Blackjack::GetPlayerName(int player)
{
  // Return the requested player's name
  return m_players[player].GetName();
}

int Blackjack::GetPlayerFunds(int player)
{
  // Return the requested player's funds
  return m_players[player].GetFunds();
}

void Blackjack::SetPlayerFunds(int player, int amt)
{
  // Set the requested player's funds
  m_players[player].SetFunds(amt);
}

bool Blackjack::SetPlayerBet(int player, int amt)
{ 
  // If the player has insufficient funds
  if(m_players[player].GetFunds() < amt)
    {
      // Return false
      return false;
    }

  // Subtract the amount from the player funds
  m_players[player].SetFunds(m_players[player].GetFunds() - amt);
  // Add the amount to the player bet
  m_players[player].SetBet(amt);
  // Return true
  return true;
}

void Blackjack::NewDeal()
{
  // Create a new unsorted 52 card deck
  Deck deck;
  // Initialize m_deck to the new deck
  m_deck = deck;
  // Shuffle m_deck
  m_deck.Shuffle();

  // 2 cards for dealer, 2 cards for each player
  int cardsToDeal = 2 + (2 * m_players.size());

  // While we still have cards to deal
  while(cardsToDeal > 0)
    {
      // Deal to each player
      for(unsigned int i = 0; i < m_players.size(); i++)
    {
      std::cout << "Deal Player Card" << std::endl;
      // Deal one card to the player
      m_players[i].GetHand().AddCard(m_deck.DealCard());
      // Decrement the number of cards to deal
      cardsToDeal--;
    }

      std::cout << "Deal Dealer Card" << std::endl;
      // Deal the dealer one card
      m_dealer.GetHand().AddCard(m_deck.DealCard());
      // Decrement the number of cards to deal
      cardsToDeal--;
    }
}

void Blackjack::OutputPlayerHand(int player)
{
  std::cout << "Player Output Card." << std::endl;
  m_players[player].GetHand().GetCard(0).OutputCard();
}

void Blackjack::OutputDealerHand()
{
  // TODO: Code Method
}

bool Blackjack::HitPlayer(int player)
{
  // TODO: Code Method
  return false;
}

void Blackjack::DealerPlay()
{
  // TODO: Code Method
}

int Blackjack::SettlePlayerBet(int player)
{
  // TODO: Code Method
  return -1;
}
#include "Player.h"

Player::Player()
{
  m_name = (char *) "Jane";
  m_funds = 100;
  m_bet = 0;
}

Player::Player(char *name, int funds)
{
  m_name = name;
  m_funds = funds;
  m_bet = 0;
}

char *Player::GetName()
{
  return m_name;
}

void Player::SetName(char *name)
{
  m_name = name;
}

int Player::GetFunds()
{
  return m_funds;
}

void Player::SetFunds(int funds)
{
  m_funds = funds;
}

int Player::GetBet()
{
  return m_bet;
}

void Player::SetBet(int bet)
{
  m_bet = bet;
}

Hand Player::GetHand()
{
  return m_hand;
}
#include "Hand.h"

void Hand::AddCard(Card card)
{
  m_cards.push_back(card);
}

Card Hand::GetCard(int card)
{
  return m_cards[card];
}

int Hand::Size()
{
  return m_cards.size();
}

void Hand::Clear()
{
  m_cards.clear();
}
下面是我的主类Proj2.cpp代码

/*
 * CHANGES TO Blackjack.h SPEC:
 *   added new member funcs:
 *     char *GetPlayerName(int)
 *     int GetNumPlayers()
 *     void OutputDealerHand()
 *
 *   HitPlayer() should print out the card that was dealt.
 */
#include <cstdlib>
#include <iostream>
#include "Blackjack.h"

using namespace std;

Blackjack *CreateGame(int argc, char *argv[]);
int ProcessArgs(int argCnt, char *args[], char **&names, int *&funds);
void DoNewDeal(Blackjack &game);
void ProcAllBets(Blackjack &game);
void DoAllPlays(Blackjack &game);
void PlayOnePlayer(Blackjack &game, int player);
void SettleAllPlayers(Blackjack &game);
void ShowAllPlayerFunds(Blackjack &game);
bool QueryAnotherRound();


int main(int argc, char *argv[]) {
    Blackjack *game;
    int round;

    cout << "Welcome to CMSC 202 Blackjack!\n";
    game = CreateGame(argc, argv);

    round = 0;
    do {
    cout << "\nRound " << ++round << ":\n";
    ProcAllBets(*game);
    DoNewDeal(*game);
    DoAllPlays(*game);
    SettleAllPlayers(*game);
    ShowAllPlayerFunds(*game);
    } while (QueryAnotherRound());

    cout << "\nGoodbye!\n";
    return 0;
}


Blackjack *CreateGame(int argc, char *argv[]) {
    char **names;
    int *funds;
    int numPlayers;
    Blackjack *game;

    numPlayers = ProcessArgs(argc - 1, &argv[1], names, funds);
    game = new Blackjack(names, numPlayers);
    for (int p = 0; p < numPlayers; p++) {
    game->SetPlayerFunds(p, funds[p]);
    }
    return game;
}


int ProcessArgs(int argCnt, char *args[], char **&names, int *&funds) {
    int i, p;
    int numRecs = argCnt / 2;

    names = static_cast<char **>(calloc(numRecs, sizeof(char *)));
    funds = static_cast<int *>(calloc(numRecs, sizeof(int)));

    for (p = 0, i = 0; p < numRecs; p++) {
    names[p] = args[i++];
    funds[p] = atoi(args[i++]);
    }
    return p;
}


void ProcAllBets(Blackjack &game) {
    int numPlayers = game.GetNumPlayers();
    int bet;

    for (int p = 0; p < numPlayers; p++) {
        cout << "How much does " << game.GetPlayerName(p) << " bet? ";
    cin >> bet;
    cout << endl;  // For neat scripting
    if (!game.SetPlayerBet(p, bet)) {
        cout << "Illegal bet--changing to $0\n";
        game.SetPlayerBet(p, 0);
    }
    }
}


void DoNewDeal(Blackjack &game) {
    int numPlayers = game.GetNumPlayers();

    game.NewDeal();
    cout << "The players' hands:\n";
    for (int p = 0; p < numPlayers; p++) {
    cout << game.GetPlayerName(p) << ": ";
    game.OutputPlayerHand(p);
    cout << endl;
    }
    cout << "Dealer: ";
    game.OutputDealerHand();  // This hides dealer's hole card
    cout << "\n\n";
}


void DoAllPlays(Blackjack &game) {
    int numPlayers = game.GetNumPlayers();
    int p;

    for (p = 0; p < numPlayers; p++) {
    PlayOnePlayer(game, p);
    }
    game.DealerPlay();
}

void PlayOnePlayer(Blackjack &game, int player) {
    char *name = game.GetPlayerName(player);
    string answer;
    bool hit, busted;

    cout << ">>" << name << "'s turn:\n";
    busted = false;
    do {
    cout << "Hand: ";
    game.OutputPlayerHand(player);
    cout << endl;

    cout << name << "'s play: ";
    cin >> answer;
    cout << endl;  // For neat scripting
    answer[0] == 'y' || answer[0] == 'Y';
    hit = (answer[0] == 'h' || answer[0] == 'H');
    if (hit) {
        busted = game.HitPlayer(player);
    }
    } while (hit && !busted);
    if (busted) {
    cout << "Busted!\n";
    }
    cout << endl;
}

void SettleAllPlayers(Blackjack &game) {
    int numPlayers = game.GetNumPlayers();
    int p;

    for (p = 0; p < numPlayers; p++) {
    game.SettlePlayerBet(p);
    // Above should print out:
    //  Joe has busted--Dealer wins", or "Sally has 15--Dealer loses"
    }
    cout << endl;
}

void ShowAllPlayerFunds(Blackjack &game) {
    int numPlayers = game.GetNumPlayers();
    int p;

    for (p = 0; p < numPlayers; p++) {
    cout << game.GetPlayerName(p) << " now has $"
         << game.GetPlayerFunds(p) << endl;
    }
    cout << endl;
}

bool QueryAnotherRound() {
    string answer;

    cout << "Another round? ";
    cin >> answer;
    cout << endl;  // For neat scripting
    return answer[0] == 'y' || answer[0] == 'Y';
}
/*
*对Blackjack.h规范的更改:
*添加了新成员函数:
*字符*GetPlayerName(int)
*int GetNumPlayers()
*void OutputDealerHand()
*
*HitPlayer()应该打印出发牌的卡片。
*/
#包括
#包括
#包括“21点.h”
使用名称空间std;
21点*CreateGame(int argc,char*argv[]);
int-ProcessArgs(int-argCnt,char*args[],char**&name,int*&funds);
void DoNewDeal(21点和游戏);
无效程序赌注(21点和游戏);
void DoAllPlays(21点和游戏);
void PlayOnePlayer(21点和游戏,int-player);
void结算玩家(21点和游戏);
void ShowAllPlayerFunds(21点和游戏);
bool QueryAnotherRound();
int main(int argc,char*argv[]){
21点*游戏;
整数轮;

cout手上的
是空的

此行:
m_players[i].GetHand().AddCard(m_deck.DealCard())
仅将卡添加到临时副本中 让
GetHand()
返回一个参考,你的玩家将实际获得牌


另外,您不应该在存储库中包含Prog2.out之类的可执行文件。

请参见和。
m_hand
在哪里将其设置为您的玩家类?您可能还希望在GetHand中将其作为引用而不是值返回。您应该尝试进入OutputPlayer并在调试器中进行调试:在我的过程中,可能会有一个指针如果你不能使用调试器,你可以把调试PROTTF语句放进去,看看它要管理哪些函数来进入,状态是什么,等等。你也会错误地把你的字符串常量投射到<代码> char */COD>:考虑为它们做存储:<代码> const char */COD>。最好还是使用<代码>std::string
来存储它们,这保证了您将副本保存到存储中,而不是依赖于其他人对存储指针的内存管理。