0

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.

  • *but whenever I try to create the vector of type Hand in my Deck class it fails.* -- Please be more specific. What do you mean by "it fails"? The second thing is that you should first practice with very simple programs on how to utilize `vector`. Waiting until you've written an entire program, and then find out you do not know how to use/initialize/etc. a vector, is not the way to learn how to use new things you are not familiar with. – PaulMcKenzie Dec 27 '22 at 02:54
  • What is the topic of the chapter in your C++ textbook that this practice problem is from? If this is a class assignment, what was the topic of the lesson where this assignment was given out? Looks like you're missing several basic C++ concepts, but it's unclear which one you're supposed to use here. If you can provide more context for this practice problem, in which context it was given, it should be possible to point you in the right direction. – Sam Varshavchik Dec 27 '22 at 02:54
  • You should move `srand(time(NULL));` to the beginning of `main` and only call it once. – Retired Ninja Dec 27 '22 at 02:59
  • Your `Deck` shouldn't have any hands or players or a dealer in it, only `Card`'s. Create a `Table` class and think about what you're trying to accomplish and how you can accomplish it. – Captain Obvlious Dec 27 '22 at 03:03

1 Answers1

1

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

bitmask
  • 32,434
  • 14
  • 99
  • 159