Home > Back-end >  Issues creating a vector of type Card inside a Deck class
Issues creating a vector of type Card inside a Deck class

Time:12-27

I'm attempting to make a simple game of blackjack in C . I have the class definitions for the Hand and Card working properly but I am struggling with creating a deck class. I can get the functionality of the deck to work in my main function but whenever I try to create the vector of type Hand in my Deck class it fails. I've worked with vectors in the past and I can make the deck functional in main but when I move the creation of the vector to a Deck class outside of main I get the following errors:

Card: undeclared identifier (Blackjack.h line 17)
'std::vector' 'Card' is not a valid template type argument for the parameter

Can someone help, thanks!

Here is my code. I attached everything just in case:

#ifndef BLACKJACK_H
#define BLACKJACK_H
#include <list>
#include <string>
#include <vector>
#include <iostream>
using namespace std;


class Deck {
    public:
        Deck();
        ///void draw(vector<Card> cards);
        void shuffle();

    private:
       vector<Card> cards;
};


class Card {
    public:
        Card();
        Card(string suit, string value);
        void setSuit(string suit);
        void setVal(string val);
        string getSuit();
        string getVal();
        void show();

    private:
        string suit;
        string val;
};

class Hand {
    public:
        void setName(string n);
        string getName();
        void addCard(Card card);
        vector<Card> cards;

    private:
        string name;

};

void total(Hand hand);

void playPlayers(vector<Card> deck, int numPlayer);

void playerDealer(vector<Hand> playerDecks, int numPlayer);


#endif
#include "Blackjack.h"

using namespace std;


Deck::Deck() {
    string suits[] = { "Spades", "Clubs", "Diamonds", "Hearts" };
    string vals[] = { "Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
    for (string s : suits) {
        for (string v : vals) {
            Card card(s, v);
            cards.push_back(card);
        }
    }
}


/*void Deck::draw(vector<Card> cards) {

}*/

void Deck::shuffle() {
    srand(time(NULL));
    for (int j = 0; j < 52; j  ) { //randomize cards
        for (int i = 0; i < 52; i  ) {
            int r = rand() % 52;
            Card temp;
            temp.setVal(cards.at(i).getVal());
            temp.setSuit(cards.at(i).getSuit());
            cards.at(i).setVal(cards.at(j).getVal());
            cards.at(i).setSuit(cards.at(j).getSuit());
            cards.at(j).setVal(temp.getVal());
            cards.at(j).setSuit(temp.getSuit());
        }
    }
}

Card::Card() {
    suit = "";
    val = "";
}

Card::Card(string s, string v) {
    suit = s;
    val = v;
}

void Card::show() {
    cout << val << " of " << suit << endl;
}

void Card::setSuit(string s) {
    suit = s;
}

void Card::setVal(string v) {
    val = v;
}

string Card::getSuit() {
    return suit;
}

string Card::getVal() {
    return val;
}

void Hand::setName(string n) {
    name = n;
}

string Hand::getName() {
    return name;
}


void Hand::addCard(Card card) {
    cards.push_back(card);
}


void total(Hand hand) {
    int total = 0;
    //for (Card card : hand.cards):

    
}

void playPlayers(vector<Hand> deck, int numPlayer) {

}

void playerDealer(vector<Hand> playerDecks, int numPlayer) {

}



#include "Blackjack.h"

int main(void) {

    Card card1("Hearts", "1");
    Card card2("Hearts", "2");

    vector <Card> deck; ///Builds deck that works, not using deck class
    string suits[] = { "Spades", "Clubs", "Diamonds", "Hearts" };
    string vals[] = { "Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
    for (string s : suits) {
        for (string v : vals) {
            Card card(s, v);
            deck.push_back(card);
        }
    }

    cout << "Welcome to Blackjack" << endl;
    int numPlayer;
    cout << "How many players are playing?" << endl;
    cin >> numPlayer;
    vector<Hand> playerDecks;
    for (int i = 0; i < numPlayer; i  ) {
        Hand player;
        playerDecks.push_back(player);
    }
    /*
    playerDecks[0].addCard(card1);
    playerDecks[0].cards[0].show();
    playerDecks[1].addCard(card2);
    playerDecks[1].cards[0].show();
    Ignore This Code
    */
    return 0;
}

I tried the exact same code in my main and in the class for Deck and it worked in main but not in deck.

CodePudding user response:

Your compiler doesn't know what a Card is supposed to be, because in the line where you want to declare your private member:

  vector<Card> cards;

the Card class has not been declared, yet. This is also the reason you can declare a vector in main.

Move the entire definition of class Card above the definition of class Deck.


Also, you should avoid the bad practice of using namespace std;.

  • Related