gpt4 book ai didi

c++ - C++ Deck 类中的段错误(核心已转储)

转载 作者:太空宇宙 更新时间:2023-11-04 15:44:43 25 4
gpt4 key购买 nike

我想从一类 Cards 中创建一个名为 Deck 的类。但是,每当我尝试查看我在 Deck 中放入的内容时,我都会收到一条错误消息,指出 Segmentation fault (core dumped)。我怎样才能解决这个问题?我从其他问题中读到我的程序可能正在尝试访问尚未分配的内存,但我不知道那发生在哪里。这是我写的代码:

甲板.h:

#ifndef DECK_H
#define DECK_H

#include <vector>

#include "card.h"

class Deck
{

friend ostream &operator<<(ostream &, Deck &);

public:
Deck();

void shuffle();

private:

vector<Card> myDeck;
};

#endif

甲板.cpp

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>

#include "deck.h"
#include "card.h"

Deck::Deck()
{

for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 13; j++)
{
Card myCard(j,i);
myDeck.push_back(myCard);
}
}
}

void Deck::shuffle()
{
for(int i = 0; i < 52; i++)
{
Card temp(1,1);
int swapID = rand() % 52;

temp = this->myDeck[i];
this->myDeck[i] = this->myDeck[swapID];
this->myDeck[swapID] = temp;
}
}

ostream &operator<<(ostream &output, Deck &theDeck)
{
for(int i = 0; i < 52; i++)
{
cout << theDeck.myDeck[i];
}
}

卡片.h:

#ifndef CARDS_H
#define CARDS_H

#include <string>

using namespace std;

class Card {

friend ostream &operator<<(ostream &, Card &);

public:
Card(int face, int suit);

void showCard();

private:

string face;
string suit;

void processFace(int value);
void processSuit(int suit);
};

#endif

卡片.cpp:

#include <iostream>
#include <string>

#include "card.h"

using namespace std;

Card::Card(int face, int suit)
{
processFace(face);
processSuit(suit);

cout << "New card created" << endl;
}

void Card::showCard()
{
cout << "This is the " << this->face << " of " << this->suit << endl;
}

void Card::processFace(int value)
{

switch(value)
{
case 1:
{
face = "Ace";
break;
}
case 2:
{
face = "2";
break;
}
case 3:
{
face = "3";
break;
}
case 4:
{
face = "4";
break;
}
case 5:
{
face = "5";
break;
}
case 6:
{
face = "6";
break;
}
case 7:
{
face = "8";
break;
}
case 9:
{
face = "9";
break;
}
case 10:
{
face = "10";
break;
}
case 11:
{
face = "Jack";
break;
}
case 12:
{
face = "Queen";
break;
}
case 13:
{
face = "King";
break;
}
}
}

void Card::processSuit(int decider)
{
switch(decider)
{
case 1:
{
suit = "Hearts";
break;
}
case 2:
{
suit = "Diamonds";
break;
}
case 3:
{
suit = "Clubs";
break;
}
case 4:
{
suit = "Spades";
break;
}
}
}


ostream &operator<<(ostream &output, Card &myCard)
{
output << myCard.face << " of " << myCard.suit << endl;
}

主要.cpp:

#include <iostream>
#include <string>
#include <vector>

#include "card.h"
#include "deck.h"

using namespace std;

int main()
{

Deck mainDeck;

cout << mainDeck << endl << endl;

}

最佳答案

你的方法

ostream &operator<<(ostream &output, Deck &theDeck)
{
for(int i = 0; i < 52; i++)
{
cout << theDeck.myDeck[i];
}
}

应该返回 ostream& ,但它没有返回任何东西。此外,您应该调用 operator<<output而不是在 cout 上(您将 cout 传递给此方法):

ostream &operator<<(ostream &output, const Deck &theDeck)
{
for(int i = 0; i < 52; i++)
{
output << theDeck.myDeck[i];
}
return output;
}

此外,请确保 operator<<对于 Card类型写得正确。

关于c++ - C++ Deck 类中的段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com