0

Deckのクラスから呼び出されるクラスを作りたいですCards。ただし、 に何を入れたかを見ようとすると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];
    }
}

card.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

card.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;
}

main.cpp:

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

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

using namespace std;

int main()
{

    Deck mainDeck;

    cout << mainDeck << endl << endl;

}
4

1 に答える 1

6

あなたの方法

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

を返す必要ostream&がありますが、何も返していません。また、 ではなく を呼び出す必要がありますoperator<<(このメソッドに渡します)。outputcoutcout

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

また、operator<<forCard型が正しく記述されていることを確認してください。

于 2013-08-24T16:47:53.720 に答える