简体   繁体   中英

Output a Vector Element of Enum Class Type in C++

I have created card and deck classes for use in a C++ poker game, however, I'm trying to check that my assignment of vector elements are correct and having issues displaying them on the console. I have an enumerated class for the suits and integer values for the ranks. I see that my deck vector is generating with 52 cards, but I'm not sure how to correctly get this to display on the screen using the card class display() function for each vector element. Any help would be appreciated.

Note: I'm also guessing an alternate method would be to overload the output stream operator to display a vector element that is an object with an enumerated class parameter, but also not sure on what that syntax would look like.

int main(){ Deck deck();}

Card.h

#pragma once
#include <stdlib.h>
#include <iostream>
using namespace std;

enum class Suit { Hearts, Diamonds, Spades, Clubs };

class Card
{
private:
    Suit m_suit;
    int m_rank;
public:
    Card();
    Card(int r, Suit s);
    void display();

    int getRank();
    void setRank(int);

    Suit getSuit();
    void setSuit(Suit);

};

Card.cpp

#include "Card.h"
#include <iostream>
#include <string>


Card::Card(): m_rank(0), m_suit(Suit::Hearts)
{
    
}


Card::Card(int r, Suit s)
{
    m_rank = r;
    m_suit = s;
}

void Card::display()
{
    if (m_rank == 1)    // for number cards, show value
    {
        std::cout << "Ace";
    }
    else if ((2 <= m_rank) && (m_rank <= 10))
    {
        std::cout << m_rank;
    }
    else
    {
        switch (m_rank)
        {
        case 11:
            std::cout << "Jack";
            break;
        case 12:
            std::cout << "Queen";
            break;
        case 13:
            std::cout << "King";
            break;
        default:
            std::cout << "INVALID RANK";

        }
    }

    switch (m_suit)         // display suit
    {
    case Suit::Hearts:
        std::cout << " of Hearts";
        break;
    case Suit::Diamonds:
        std::cout << " of Diamonds";
        break;
    case Suit::Spades:
        std::cout << " of Spades";
        break;
    case Suit::Clubs:
        std::cout << " of Clubs";
        break;

    };
}

int Card::getRank()
// return the numeric value of a card
{
    return (m_rank);
}

Deck.h

#pragma once
#include <vector>
#include "Card.h"
class Deck
{
private:
    Card card;
    std::vector<Card> m_deck;
public:
    Deck();
    void shuffle();
    void grabCard();
};

Deck.cpp

#include "Deck.h"
#include "Card.h"
#include <vector>

Deck::Deck()
{
    
    for (int i = 1; i < 14; i++)
    {
        for (int j = int(Suit::Hearts); j <= int(Suit::Clubs); j++)
        {
            m_deck.push_back(Card(int(i), Suit(j))); //passing the card constructor to create a card object, then store it as a vector element
            int counter = 0; // use to print out each element
            std::cout << m_deck[card.display()]; //output each card obect via display method
            std::cout << "\t\n";
            counter++;
        }
    }

}
void Deck::shuffle()
{
    

}
void Deck::grabCard()
{

}

To display vector element, you should call display() of the vector element to display.

std::cout << m_deck[card.display()];

should be

m_deck.back().display();

to have it display the last pushed card.

Also int main(){ Deck deck();} is a main function with only function declaration and Deck::Deck() won't be called.
To call the constructor, it should be int main(){ Deck deck;} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM