Card Game

Hi there,

I'm working on a school project and I've gotten stuck and I was hoping for some guidance. I've been writing testers for each of my files and things were going pretty well until I hit inheritance and now I can't use my vector functions even though I've included <vector>

There are several parts but this game has 3 header files (a Card, Pile, and Deck). The game is a version of War.

The Deck is supposed to publicly inherit all functions and members from the Pile class.

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
 
#pragma once
#include "Card.h"
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <iterator>
using namespace std;

class Pile {

private:
	vector<Card> deck; //declares a vector of Cards
public:
	Pile(); //default pile maker
	Card dealCard();

	int getCount();
	void shuffle();
	void clear();

	void Pile::operator+ (const Card& c); 
	void Pile::operator+ (const Pile& p);
	friend ostream& operator<< (ostream& out, const Pile& obj);
	
};



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
41
42
43
44
45
46
47
48
49
50
51
52
53
 
#include "Card.h"
#include "Card.cpp"
#include "Pile.h"
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <random>
#include <iomanip>
using namespace std;

Pile::Pile() {
	vector<Card> deck; //declare vector of Cards
}
Card Pile::dealCard() {
	return deck[0];
}
int Pile::getCount() {
	return deck.size();
}
void Pile::shuffle() {
	//auto rng = default_random_engine{};
	return random_shuffle(deck.begin(), deck.end());
}
void Pile::clear() {
	return deck.clear();
}
void Pile::operator+ (const Card& c) {
	deck.push_back(c);
}
void Pile::operator+ (const Pile& p) {
	deck.insert(deck.end(), p.deck.begin(), p.deck.end());
}
//overloaded << operator
ostream& operator<< (ostream& out, const Pile& obj) {
	for (int i = 0; i < obj.deck.size(); i++) {
		out << obj.deck[i] << setw(5);
		//if(obj.deck[i] == 9)
	}
	return out;
}
/*test it
int main() {
	Pile gameDeck = Pile();
	Pile player = Pile();

	cout << gameDeck << endl;
	cout << "GetCount: " << gameDeck.getCount() << endl;

	system("pause");
}*/



1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#pragma once
#include "Card.h"
#include "Pile.h"
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <iterator>
using namespace std;

class Deck : public Pile {
public:
	Deck();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#include "Card.h"
#include "Pile.h"
#include "Deck.h"
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <iterator>
using namespace std;

Deck::Deck() {
	Pile gameDeck = Pile();
	//vector<Card> gameDeck;

	for (int i = 0; i <= 13; i++) {
		gameDeck.push_back(Card(i, 'A'));
		gameDeck.push_back(Card(i, 'S'));
		gameDeck.push_back(Card(i, 'H'));
		gameDeck.push_back(Card(i, 'C'));
	}
}


^push_back has red squiggles and is bringing up error "class Pile has no memeber "push_back". It goes away when I use vector<Card> gameDeck so it's got to be something with my Pile or how I've inherited it right?

Okay so I've made progress. But Deck won't use any of the functions from pile

my new deck.cpp
1
2
3
4
5
6
7
8
9

Deck::Deck() {
	for (int i = 1; i <= 13; i++) {
		gameDeck + Card(i, 'A');
		gameDeck + Card(i, 'S');
		gameDeck + Card(i, 'H');
		gameDeck + Card(i, 'C');
	}
}

deck.h
1
2
3
4
5
6
class Deck : public Pile {
private:
	Pile gameDeck;
public:
	Deck();
};


and my pile.cpp which has the functions I need to use in deck
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
Pile::Pile() {
	pile; //declare vector of Cards
}
Card Pile::dealCard() {
	return pile[0];
}
int Pile::getCount() {
	return pile.size();
}
void Pile::shuffle() {
	return random_shuffle(pile.begin(), pile.end());
}
void Pile::clear() {
	return pile.clear();
}
void Pile::operator+ (const Card& c) {
	pile.push_back(c);
}
void Pile::operator+ (const Pile& p) {
	pile.insert(pile.end(), p.pile.begin(), p.pile.end());
}
//overloaded << operator
ostream& operator<< (ostream& out, const Pile& obj) {
	for (int i = 0; i < obj.pile.size(); i++) {
		out << obj.pile[i] << setw(5);
		//if(obj.pile[i] == 9)
	}
	return out;
}


So when I try this it gives me nothing:
1
2
3
4
5
6
int main() {
	Deck newDeck = Deck();
	cout << newDeck << endl;

	system("pause");
}

It should be outputting the whole deck of cards shouldn't it? I have the operator<< overloaded in my Pile.ccp which is inherited by Deck.

Your << operator works with the contents of the variable named pile.

What's in pile? I see that your Deck constructor creates an object named gameDeck, populates it, and then throws it away. So what's meant to be in pile?
I messaged my professor and they cleared things up, my deck.cpp is supposed to be using deference this
1
2
3
4
5
6
7
8
Deck::Deck() {
	for (int i = 1; i <= 13; i++) {
		*this + Card(i, 'D');
		*this + Card(i, 'S');
		*this + Card(i, 'H');
		*this + Card(i, 'C');
	}
}

So I don't need to be making gameDeck in my .h file.

But in Pile is a vector of Cards, so I have a Card.h and Card.cpp files that make a card with rank and suit. Then use that to make a vector of Cards for my Pile which will be used to handle the opponents hands.

Thank you for the response Repeater, I should have this one figured out now. It was just that *this that I got stuck on.
Topic archived. No new replies allowed.