Passing vector by reference

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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

const int number_of_cards = 52;

class DeckOfCards {
public:
	void shuffleDeck(std::vector<std::string> &);
}
DeckOfCards::shuffleDeck(std::vector<std::string> &theDeck) {
	random_shuffle(theDeck.begin(), theDeck.end());
}


int main()
{
	DeckOfCards DeckObject;

	std::vector<std::string>theDeck(number_of_cards);

	for(unsigned int i = 0; i < theDeck.size(); i++)
	{
		// Populates the vector with the standard deck of card
	}

	DeckObject.shuffleDeck(&theDeck);


	std::cin.ignore();
 	return 0;
}


My compiler reads:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
1>Compiling...
1>main.cpp
1>c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(12) : error C2556: 'DeckOfCards DeckOfCards::shuffleDeck(std::vector<_Ty> &)' : overloaded function differs only by return type from 'void DeckOfCards::shuffleDeck(std::vector<_Ty> &)'
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>        c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(10) : see declaration of 'DeckOfCards::shuffleDeck'
1>c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(12) : error C2371: 'DeckOfCards::shuffleDeck' : redefinition; different basic types
1>        c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(10) : see declaration of 'DeckOfCards::shuffleDeck'
1>c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(50) : error C2664: 'DeckOfCards::shuffleDeck' : cannot convert parameter 1 from 'std::vector<_Ty> *' to 'std::vector<_Ty> &'
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>Build log was saved at "file://c:\Users\Robert Cruz\Documents\Visual Studio 2008\Projects\war solution\war project\Debug\BuildLog.htm"
1>war project - 3 error(s), 0 warning(s)

Can anyone help me spot my error?

Thanks,
Robert.
Last edited on
uhum... you need to actually read these messages. Your DeckOfCards class has two shuffleDeck methods with the same signature.
closed account (1vRz3TCk)
1
2
3
4
5
6
7
8
9
10
class DeckOfCards 
{
public:
	void shuffleDeck(std::vector<std::string> &);
};

void DeckOfCards::shuffleDeck(std::vector<std::string> &theDeck) 
{
	random_shuffle(theDeck.begin(), theDeck.end());
}
??
How do I fix this? It's my first time working with classes. Apologies.
Yes, that did fix most of the errors. Great thanks.

What does this error mean:
cannot convert parameter 1 from std::vector<_Ty> * to std::vector<_Ty> &
Last edited on
Hi - there are just a few typos stopping this from working.

Semi-colon missing at end of your class declaration.
Definition of shuffleDeck has no (return) type (- it's void in your prototype).
Your function call (to shuffleDeck) in main is wrong: &theDeck is the address of theDeck.

Also, from a design point of view, it'd be better for the DeckOfCards class to hold the deck of cards ;)

Regards, keineahnung
You guys are awesome! Thanks for the help.
@keineahnung: Is this what you meant?
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
##include <iostream>
#include <string>
#include <algorithm>
#include <vector>

const int NUM_OF_CARDS = 52;

class DeckOfCards 
{
private:
	std::vector<std::string>theDeck();
public:
	void fillDeckVector(std::string, std::string);
};
void DeckOfCards::fillDeckVector(std::string s[], std::string r[])
{
	theDeck(NUM_OF_CARDS);
	// Populates vector
}

int main()
{
	DeckOfCards DeckObject;

	std::string sASuit[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
	std::string sARank[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

	DeckObject.fillDeckVector(sASuit, sARank);

	
	std::cin.ignore();
 	return 0;
}


:/ I just keep getting compiler error:
1
2
3
4
5
6
7
8
9
10
11
1
1>war project : warning PRJ0009 : Build log could not be opened for writing.
1>Make sure that the file is not open by another process and is not write-protected.
1>Compiling...
1>main.cpp
1>c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(23) : error C2511: 'void DeckOfCards::fillDeckVector(std::string [],std::string [])' : overloaded member function not found in 'DeckOfCards'
1>        c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(9) : see declaration of 'DeckOfCards'
1>c:\users\robert cruz\documents\visual studio 2008\projects\war solution\war project\main.cpp(49) : error C2664: 'DeckOfCards::fillDeckVector' : cannot convert parameter 1 from 'std::string [4]' to 'std::string'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>Build log was saved at "file://c:\Users\Robert Cruz\Documents\Visual Studio 2008\Projects\war solution\war project\Debug\BuildLog.htm"
1>war project - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
1
2
3
4
5
6
7
class DeckOfCards 
{
private:
	std::vector<std::string>theDeck();
public:
	void fillDeckVector(std::string, std::string[]);
};


Pay attention to what you're doing and make sure you understand why you're doing it.
@Da0omph Sep 12, 2011 at 11:02am
@keineahnung: Is this what you meant?

Hmmm, yes and no.
Your DeckOfCards class should be initializing itself in its constructor. Also, how are you going to represent a single playing card? That's not a single (string) value but two: a suit and a rank. So I'd be inclined to nest a struct inside the DeckOfCards class to model that, and then have a vector of those. And are strings the best way of storing the suits and ranks? What about enums?
If you think about how you want to use your class when it's finished, it might help you with the design stage. E.g.

1
2
3
4
DeckOfCards my_deck = DeckOfCards();
my_deck.shuffle();
int rank = my_deck.cut();
Hand my_hand = my_deck.deal();


Etc. :)
What's the book you're learning from, BTW? Does it have a section on class design?
Regards, keineahnung




Well, this is how I'm doing it. Tell me if I'm just over thinking this project way too much.
I got a string vector of 52, a normal deck of card. I'm then splitting it into two other string vectors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void DeckOfCards::splitDeck(std::vector<std::string>& theDeck, std::vector<std::string>& playerHand1, std::vector<std::string>& playerHand2)
{
	int x = 1;
	int y = 1;
	playerHand1[0] = theDeck[0];
	playerHand2[0] = theDeck[1];
	for(unsigned int i = 2; i < theDeck.size(); i++)
	{
		if((i % 2) == 0)
			playerHand1[x++] = theDeck[i];
		else
			playerHand2[y++] = theDeck[i];
	}
}


Then I'm taking the top card in the vectors and comparing the the first word in the string.
i.e. Ace of Spaces. It takes Ace and compares all the index positions in this array.
{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
Finds the match, and records the matching index position in another int array that is parallel in order to find the actual value.

Eventually, I'm going to have a game of I declare war.
Last edited on
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <ctime>

const int NUM_OF_CARDS = 52;

class DeckOfCards 
{
private:
	std::string str, str2;
public:
	void fillDeckVector(std::vector<std::string>&, std::string[], std::string[]);
	void splitDeck(std::vector<std::string>&, std::vector<std::string>&, std::vector<std::string>&);
	int foo(std::string, std::string[]);
};
void DeckOfCards::fillDeckVector(std::vector<std::string>& theDeck, std::string s[], std::string r[])
{
	int x = 0; int y = 0;
	for(unsigned int i = 0; i < theDeck.size(); i++)
	{
		theDeck[i] = r[x++] + " of " + s[y++];
		if(x == 13)
			x = 0;
		else if(y == 4)
			y = 0;
	}

	srand(static_cast<int>(time(NULL)));
	for(int i = 0; i < rand() % 50; i++)
		random_shuffle(theDeck.begin(), theDeck.end());
}
void DeckOfCards::splitDeck(std::vector<std::string>& theDeck, std::vector<std::string>& playerHand1, std::vector<std::string>& playerHand2)
{
	int x = 1;
	int y = 1;
	playerHand1[0] = theDeck[0];
	playerHand2[0] = theDeck[1];
	for(unsigned int i = 2; i < theDeck.size(); i++)
	{
		if((i % 2) == 0)
			playerHand1[x++] = theDeck[i];
		else
			playerHand2[y++] = theDeck[i];
	}
}

int DeckOfCards::foo(std::string strP1, std::string r[]){
	str = strP1.erase(strP1.find(" ", 0));
	for(int i = 0; i < 13; i++)
	{
		if(str == r[i])
		{
			std::cout << "strP1 is: " << strP1 << std::endl;
			std::cout << "r[i] is: " << r[i] << std::endl;
			std::cout << "Yes, strings match." << std::endl;
			return i;
		}
	}
}

int main()
{
	DeckOfCards DeckObject;

	std::vector<std::string>theDeck(NUM_OF_CARDS);
	std::vector<std::string>playerHand1(26);
	std::vector<std::string>playerHand2(26);

	std::string sASuit[4]  = {"Clubs", "Diamonds", "Hearts", "Spades"};
	std::string sARank[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
	int iAValue[13]        = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

	DeckObject.fillDeckVector(theDeck, sASuit, sARank);
	DeckObject.splitDeck(theDeck, playerHand1, playerHand2);

	int returnValue = DeckObject.foo(playerHand1[0], sARank);
	std::cout << "The return value from the compare string function was: " << returnValue << std::endl;
	std::cout << "The interger value for " << playerHand1[0] << " is " << iAValue[returnValue] <<std::endl;

	std::cout << std::endl << playerHand1[0] << " beats ";
	for(int i = returnValue; i > 0; i--)
		std::cout << sARank[i] << " ";


	std::cin.ignore();
 	return 0;
}

Here's the whole thing. It seems like this could be much easy, but I'm over thinking.
I have no books. All I know is from this site.
Last edited on
Topic archived. No new replies allowed.