Vector Array and Functions

So I am trying to pass a deck of cards to a function then the function uses the deck to fill a vector array with people * 3 (3 cards per person and people is the amount of people playing). My problem is that I cant access the vector array after the function ends. I tried passing a vector array but then after the function ends it doesnt save the values. Do I need to return the vector array? If so how do I do that.

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
// compiles
// try passing a vector
void dealPalace(int deck[], vector<int> palaceCards, const int people)
{
	for (int i = 0; i < 3 * people; i++)
	{
		palaceCards[i] = deck[i];
		cout << palaceCards[i] << "*";
		deck[i] = 999;
	}// end for
}// end dealPalace


// does not compile
// without passing vector
int dealPalace(int deck[], const int people)
{
        vector<int> palaceCards(people * 3);

	for (int i = 0; i < 3 * people; i++)
	{
		palaceCards[i] = deck[i];
		cout << palaceCards[i] << "*";
		deck[i] = 999;
	}// end for
        return palaceCards; // ** compile problem here
}// end dealPalace 


I have also tried void versions of the 2nd and int versions of the first etc. I cant really find any good documentation on vectors that says how to pass them and use them after the function ends. I just find the basics.
If you want to keep the values in the vector after the function has finished you could just pass the vector by reference.

http://www.cplusplus.com/doc/tutorial/functions2/
vector<int> &palaceCards
ah ok. So should I be passing the deck array by reference as well?
also what if I wanted to create a vector inside the function and access it later. Would I do that by returning a reference?
Depends exactly what you need to do.

If you want to create a vector inside a function and then pass it to main, you will have to return it, something like this. The problem with your function above is you are returning a vector, however you have the function return type as an int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vector<int> Function()
{
	vector<int> vec;

	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);

	return vec; 
}

int main()
{
    // Puts the results of the function inside the vector
    vector<int> vec(Function());   

    for(int i = 0; i < vec.size(); i++)
    {
	   cout << vec[i] << endl;   // Prints out 1,2,3
    }


If you want to create the vector inside main and change the values in the function, you can use references with &.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Function(vector<int> &vec)
{
	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);
}

int main()
{
       vector<int> vec;

        // Keeps the values the function gives, as passed by reference
	Function(vec);  

	for(int i = 0; i < vec.size(); i++)
	{
		cout << vec[i] << endl;  // Prints out 1,2,3;
	}
hmm ok so what am i doing wrong here. Sorry for the spam of code in advance... but I dont understand the error. I posted the code below but ive also tried the void statement with 4 different statements (aka one with playerOneHand then another with playerOneHand and playerTwoHand.. etc. But i only wanted to post one of the functions.. Ive also tried doing it with one function only, same result.

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

switch (PLAYERS)
	{
		case 2:
		{
			vector<int> playerOneHand(6,1);
			vector<int> playerTwoHand(6,1);
			dealHands(deck, playerOneHand, playerTwoHand, PLAYERS);
		}
		case 3:
		{
			vector<int> playerOneHand(6,1);
			vector<int> playerTwoHand(6,1);
			vector<int> playerThreeHand(6,1);
			dealHands(deck, playerOneHand, playerTwoHand, playerThreeHand, PLAYERS);
		}
		case 4:
		{
			vector<int> playerOneHand(6,1);
			vector<int> playerTwoHand(6,1);
			vector<int> playerThreeHand(6,1);
			vector<int> playerFourHand(6,1);
			dealHands(deck, playerOneHand, playerTwoHand, playerThreeHand, playerFourHand, PLAYERS);
		}
		default:
			{
				vector<int> playerOneHand(6,1);
				dealHands(deck, playerOneHand, PLAYERS);
			}
	}// end switch
}


void dealHands(int deck[],vector<int> &playerOneHand, vector<int> &playerTwoHand, vector<int> &playerThreeHand, vector<int> &playerFourHand, const int PLAYERS)
{
}

1>palaceGame.obj : error LNK2019: unresolved external symbol "void __cdecl dealHands
(int * const,class std::vector<int,class std::allocator<int> >,int)"
(?dealHands@@YAXQAHV?$vector@HV?$allocator@H@std@@@std@@H@Z) referenced in function "void __cdecl playGame(int * const,int)"
(?playGame@@YAXQAHH@Z)
1>palaceGame.obj : error LNK2019: unresolved external symbol "void __cdecl
 dealHands(int * const,class std::vector<int,class std::allocator<int> >,class 
std::vector<int,class std::allocator<int> >,class std::vector<int,class 
std::allocator<int> >,class std::vector<int,class std::allocator<int> >,int)"
 (?dealHands@@YAXQAHV?$vector@HV?$allocator@H@std@@@std@@111H@Z) 
referenced in function "void __cdecl playGame(int * const,int)" (?
playGame@@YAXQAHH@Z)
1>palaceGame.obj : error LNK2019: unresolved external symbol "void __cdecl 
dealHands(int * const,class std::vector<int,class std::allocator<int> >,class 
std::vector<int,class std::allocator<int> >,class std::vector<int,class 
std::allocator<int> >,int)" (?dealHands@@YAXQAHV?$vector@HV?
$allocator@H@std@@@std@@11H@Z) referenced in function "void __cdecl 
playGame(int * const,int)" (?playGame@@YAXQAHH@Z)
1>palaceGame.obj : error LNK2019: unresolved external symbol "void __cdecl 
dealHands(int * const,class std::vector<int,class std::allocator<int> >,class 
std::vector<int,class std::allocator<int> >,int)" (?dealHands@@YAXQAHV?
$vector@HV?$allocator@H@std@@@std@@1H@Z) referenced in function "void 
__cdecl playGame(int * const,int)" (?playGame@@YAXQAHH@Z)
1>C:\Users\Craig\Documents\C++\Palace\Debug\palace.exe : fatal error 
LNK1120: 4 unresolved externals
1>


Last edited on
The prototype probably doesn't match the implementation. Did you forget to change the prototype?
Edit: You were right, I forgot the & in the prototype. Thank you
Last edited on
Topic archived. No new replies allowed.