Blackjack ERRORS!

can someone please help me? I am stuck just trying to make the deck. My partner and I have been working on this for about two weeks now and I have decided to pretty much scrap the other code and make a new one. The project is due tonight (I know good timing huh) and I don't understand why it keeps telling me that 'class Deck has no member named push.'

Any help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Blackjack
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <ctime>

using namespace std;

string suit[4] = { "CLUBS", "DIAMONDS", "HEARTS", "SPADES" };
string value[13] = { "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING" };

struct Card {
	int value, suit;
	Card(int value, int suit);
	string toString();
	};

class Deck{
	public:
		stack<Card> deck;
	};
	
int main(){
	Deck deck;
	
	for(int i = 0; i < 13; i++){
		for(int j = 0; j < 4; j++){
			Card card(i, j);
			deck.push(card);
			}
		}



	return 0;
	}
It tells you that because you, quite literally, don't have a member function in 'Deck' called 'push'. If you want to call the push() function that is a member of a stack, you need to specify the object named 'deck' that's inside of class 'Deck'.

What you need to do is:
 
deck.deck.push(card);


OR, define a function in class 'Deck' that does that same thing.

1
2
3
4
5
6
class Deck
{
public:
    stack<Card> deck;
    void addCard(Card c) { deck.push(c); }
}

1
2
//then you say
deck.addCard(card);
Last edited on
Okay, thank you for your help! That solved that problem, but now I get an error saying 'undefined reference to Card::Card(int, int)' collect2 Id: returned 1 exit status.

I'm sorry for my ignorance in this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Blackjack
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <ctime>

using namespace std;

string suit[4] = { "CLUBS", "DIAMONDS", "HEARTS", "SPADES" };
string value[13] = { "ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING" };

struct Card {
	int value, suit;
	Card(int value, int suit);
	string toString();
	};

class Deck{
	public:
		stack<Card> deck;
		void addCard(Card c) { deck.push(c); }
	};
	
int main(){
	Deck deck;
	
	for(int i = 0; i < 13; i++){
		for(int j = 0; j < 4; j++){
			Card card(i, j);
			deck.addCard(card);
			}
		}



	return 0;
	}
Can I see the code where you defined the constructor of Card?
1
2
3
4
5
6
7
struct Card {
	int value, suit;
	Card(int value, int suit); //Did you define this?
        //perhaps you want to do this:
        Card(int v, s) : value(v), suit(s);
	string toString();
	};
All the code I have is above... hang on, I think I messed up a little, I just caught it. Let me change that and see if it works and I'll get back quickly. Thank you
Ok, What I tried didn't work either lol;

I see where you are asking did I define value and suit and what I thought is that is seems like I defined value and suit two different times. I wasn't meaning to at all. I just need one variable right?

Here's what I did so it will be more clear:

Before:
1
2
3
4
5
struct Card {
	int value, suit;
	Card(int value, int suit);
	string toString();
	};


After:
1
2
3
4
5
struct Card {
	int value, suit;
	Card(value, suit);
	string toString();
	};
Yeah, you did define the variables twice. You're trying to make a constructor to call to set the values of 'value' and 'suit'. As of right now all you've done is prototype the constructor. So you can pass it those values, but it doesn't do anything with them. Change your card struct to the following:
1
2
3
4
5
6
struct Card
{
    int value, suit;
    Card(int v, int s) : value(v), suit(s); //sets the the values of 'value', and 'suit' to 'v' and 's'.
   string toString();
};
Ok I promise I'm not asking you to write this program for me, but I obviously don't understand what I'm supposed to be understanding. Whenever I change it to what you wrote, it is giving me error: expected '{' at the end of (that line). What am I supposed to do with this now? I'm sorry for taking so much time to help me and thank you for donating that time to me. It is really appreciated.
Oops, i'm sorry, i made a mistake. That's what I get for trying to reply swiftly.
it should be:
1
2
3
4
5
6
struct Card
{
    int value, suit;
    Card(int v, int s) : value(v), suit(s) {} //sets the the values of 'value', and 'suit' to 'v' and 's'.
   string toString();
};


What that does is set 'value' and 'suit', the two ints in the struct 'Card', to the values you pass to the constructor.
It should all work from there.
Last edited on
Topic archived. No new replies allowed.