Error when passing vector as parameter of a function.

When I try to call a function that has a vector as a parameter, it produces an error. When I used the name of the vector, it gives the error: "more than one instance of overloaded function "function" matches the argument list". If I use vector[], it gives the error: "expected an expression".

This is a portion of my code.
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
int main()
{
	bool replay_answer = true;
	int num_chips = 1;
	while(replay_answer==true)
	{
		menu(num_chips, replay_answer);
		system("pause");
	}
	return(0);
}

void menu(int &num_chips, bool &replay_answer)
{
	vector<int> money(0);
	int money_now = 0;
	string menu_choice;
	cout << "Welcome to Plinko.  This is a game that will test your luck.  Please pick one of four options:"<<endl;
	cout << "Drop one chip."<<" To pick this option, type 'chip'."<<endl;
	cout << "Drop multiple chips."<<" To pick this option, type 'chips'."<<endl;
	cout << "Drop one or more chips in all nine slots."<<" To pick this option, type 'slots'."<<endl;
	cout << "To quit, press q."<<endl;
	cin >> menu_choice;
	menu_analysis(menu_choice, num_chips, replay_answer, money_now, money);
}

void menu_analysis(string menu_choice, int &num_chips, bool &replay_answer, int &money_now, vector<int>& money)
{
	int decision;
	if(menu_choice == "chip")	
	{
		decision = 1;
	}
	else if(menu_choice == "chips")
	{
		decision = 2;	
	}
	else if(menu_choice == "slots")
	{
		decision = 3;
	}
	else if(menu_choice == "q")
	{
		quit(replay_answer); //This ends the program
	}
	else 
	{
		quit(replay_answer);
	}
	drop_chips(decision, num_chips, replay_answer, money_now, money);
}

The rest of the code defines more functions that give the same error.
Do you have more than one definition of the functions?
Last edited on
not anymore. I rearranged it and it got rid of the multiple definitions, but now it gives drop_chips the error of "overloaded function". Does that just mean I have too many parameters?
Last edited on
What's the function prototype?
I'm not sure I know what that means. I new to C++, and somewhat new to programming.
This is the full program. Sorry, I know it's kind of long, but this has been edited from above and almost works, but it gives drop_chips the error of "ambiguous call to overloaded function"
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
#include <ctime>
using namespace std;

void drop_chips(int val, int &chips, bool &ans, int &money, vector<int> mon);
void menu(int &chips, bool &ans);

void quit(bool &replay_answer)
{
	cout << "Game Over."<<endl;
	system("pause");
	replay_answer = false;
	exit(0);  //This ends the program
}

double stay_on_board(double &slot_num, double &location_change)
{
	if (slot_num<0)
	{
		location_change = 1.0;
		slot_num = slot_num + location_change;
		cout<<slot_num<<endl;
	}
	else if(slot_num>8)
	{
		location_change = -1.0;
		slot_num = slot_num + location_change;
		cout<<slot_num<<endl;
	}
	return slot_num;
}

int win_money(double slot_num, int &num_chips, bool &replay_answer, int decision, vector<int>& money)
{
	int money_won = 0;
	if(slot_num == 0)
	{
		money_won = money_won + 100;
	}
	else if(slot_num == 1)
	{
		money_won = money_won + 500;
	}
	else if(slot_num == 2)
	{
		money_won = money_won + 1000;
	}
	else if(slot_num == 3)
	{
		money_won = money_won + 0;
	}
	else if(slot_num == 4)
	{
		money_won = money_won + 10000;
	}
	else if(slot_num == 5)
	{
		money_won = money_won + 0;
	}
	else if(slot_num == 6)
	{
		money_won = money_won + 1000;
	}
	else if(slot_num == 7)
	{
		money_won = money_won + 500;
	}
	else if(slot_num == 8)
	{
		money_won = money_won + 100;
	}
	//system("pause");
	money.push_back(money_won);
	return money_won;
}

void repeat_drop(int decision, int &num_chips, bool &replay_answer, int &money_now, vector<int>& money)
{
	string repeat;
	//this asks if the user wants to replay, and gain more money;
	cout << "Would you like to drop more chips? y or n.  If you say no, you will lose all your money."<<endl;
	cin >> repeat;
	if(repeat=="y")
	{
		drop_chips(decision, num_chips, replay_answer, money_now, money);
	}
	else if (repeat == "n")
	{
		menu(num_chips, replay_answer);
	}
	else //this ends the program after the user inputs a non-usable string
	{
		quit(replay_answer);
	}
}

void count_money(double slot_num, int &num_chips, bool &replay_answer, int decision, int &money_now, vector<int>& money)
{
	int money_won = win_money(slot_num, num_chips, replay_answer, decision, money);
	money_now = money_won + money_now;
	cout<<"You won $"<<money_won<<" with this chip.  You now have $"<<money_now<<"."<<endl;
	system("pause");
}

double slot_change()
{
	double direction = 0.5-(rand()*1.0/RAND_MAX); //random number between -0.5 and 0.5
	double location_change;
	if (direction > 0)
	{
		location_change = 0.5;
	}
	else if (direction < 0)
	{
		location_change = -0.5;
	}
	return location_change;
}

void money_analysis()
{
}

void track_chips(double slot_num, int &num_chips, bool &replay_answer, int decision, int &money_now, vector<int>& money)
{
	for(int chips_used=0; chips_used<num_chips; chips_used++) //this does the loop until the chips are used up (if num_chips==1, only does it once)
	{
		cout<<"This is chip "<<chips_used+1<<"."<<endl;
		for(int num_pegs=0; num_pegs<12; num_pegs++) //redoes for twelve pegs
		{
			double location_change = slot_change();
			slot_num = slot_num + location_change; //changes location			
			if(slot_num>=0 && slot_num <=8)
			{
				cout<<slot_num<<endl; //continues
			}
			else
			{
				slot_num = stay_on_board(slot_num, location_change);
			}
		}
		count_money(slot_num, num_chips, replay_answer, decision, money_now, money);
	}	
}

void drop_all_slots(int &num_chips, bool &replay_answer, int &money_now,  int &decision, vector<int>& money)
{
	for(int slot_drop=0; slot_drop<9; slot_drop++)
	{
		double slot_num = slot_drop;
		cout<<"Dropping "<<num_chips<<" chips into Slot #"<<slot_num+1<<endl;
		system("pause");		
		track_chips(slot_num, num_chips, replay_answer, decision, money_now, money);
		money_analysis();
	}
}

void drop_chips(int decision, int &num_chips, bool &replay_answer, int &money_now, vector<int>& money)
{
	if(decision==2 || decision==3) //multiple chips
	{	
		cout<<"How many chips do you want to drop?"<<endl;
		cin >> num_chips; //chooses how many chips the user wants to drop in a spot
		if(num_chips<=0 || cin.fail())
		{
			//returns to menu
			menu(num_chips, replay_answer);
		}
	}
	if(decision==1 || decision==2)
	{
		double slot_num;
		cout << "Where do you want to drop the chips? Please type a number between 0 and 8."<<endl;
		cin >> slot_num; 
		if(slot_num<0 || slot_num>8 || cin.fail())
		{
		menu(num_chips, replay_answer);
		}
		track_chips(slot_num, num_chips, replay_answer, decision, money_now, money);
	}
	if(decision==3)
	{
		drop_all_slots(num_chips, replay_answer, money_now, decision, money);
	}
	repeat_drop(decision, num_chips, replay_answer, money_now, money);
}

void menu_analysis(string menu_choice, int &num_chips, bool &replay_answer, int &money_now, vector<int>& money)
{
	int decision;
	if(menu_choice == "chip")	
	{
		decision = 1;
	}
	else if(menu_choice == "chips")
	{
		decision = 2;	
	}
	else if(menu_choice == "slots")
	{
		decision = 3;
	}
	else if(menu_choice == "q")
	{
		quit(replay_answer); //This ends the program
	}
	else
	{
		quit(replay_answer);
	}
	drop_chips(decision, num_chips, replay_answer, money_now, money);
}

void menu(int &num_chips, bool &replay_answer)
{
	vector<int> money(0);
	int money_now = 0;
	string menu_choice;
	cout << "Welcome to Plinko.  This is a game that will test your luck.  Please pick one of four options:"<<endl;
	cout << "Drop one chip."<<" To pick this option, type 'chip'."<<endl;
	cout << "Drop multiple chips."<<" To pick this option, type 'chips'."<<endl;
	cout << "Drop one or more chips in all nine slots."<<" To pick this option, type 'slots'."<<endl;
	cout << "To quit, press q."<<endl;
	cin >> menu_choice;
	menu_analysis(menu_choice, num_chips, replay_answer, money_now, money);
}

int main()
{
	bool replay_answer = true;
	int num_chips = 1;
	while(replay_answer==true)
	{
		menu(num_chips, replay_answer);
		system("pause");
	}
	return(0);
}
Last edited on
The function prototype is the one you write stating the return type, name and parameters, without writing what the function does
Example

1
2
3
4
5
6
7
8
9
10
// Prototype
void Foo(int, char);

// Definition
void Foo(int n, char c)
{
  for(int i = 0; i < n; ++i)
    cout << c;
}
okay, yeah, I had those, and I think I did them wrong. Let me check that. Thanks.
Yes, from the code you posted you can see that the prototype and definition of drop_chips() have different parameters
I fixed the prototypes, but it still has problems.
If I include all the prototypes, it still gives the "more than one instance of overloaded function "function" matches the argument list" error. If I only include two, then it just gives me "ambiguous call to overloaded function" for those two
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//these are all the prototypes that give the first error
void quit(bool);
double stay_on_board(double, double);
int win_money(double, int, bool, int, vector<int>);
void repeat_drop(int, int, bool, int, vector<int>);
void count_money(double, int, bool, int, int, vector<int>);
double slot_change();
void money_analysis();
void track_chips(double, int, bool, int, int, vector<int>);
void drop_all_slots(int, bool, int, int, vector<int>);
void drop_chips(int, int, bool, int, vector<int>);
void menu_analysis(string, int, bool, int, vector<int>);
void menu(int, bool);

//these are the necessary prototypes that give the second error
void drop_chips(int, int, bool, int, vector<int>);
void menu(int, bool);

Am I missing something basic?
Last edited on
If I include all the prototypes... Am I missing something basic?


Yes. You need to correct the prototype/declarations to reflect the actual definitions.

For instance, declaration:
void drop_chips(int, int, bool, int, vector<int>);

definition:
void drop_chips(int decision, int &num_chips, bool &replay_answer, int &money_now, vector<int>& money)

You'll notice that the types for the 2nd, 3rd, 4th and 5th parameters do not match between the declaration and the definition.

Correct declaration:
void drop_chips(int, int &, bool &, int &, vector<int>&)

In the fulll code you posted above, only the last parameters differed.

Last edited on
Thank you so much. That fixed the program. Thank you, I really appreciate it.
Looks to me like all your prototypes take their parameters by value, while in your definitions they take them by reference
Even if the syntax to pass the arguments to a function call is the same in both cases, what happen are two very different things

I'm not sure of this but
more than one instance of overloaded function "function" matches the argument list

is a response to the fact that since your definitions take different parameters (references instead of values) they are treated like an overload of the prototypes (which are not defined, since the definitions are treated like different functions).
Note that a function definition can also act as a prototype, so what happens is supposed to happen
The compiler doesn't know which version to call, because the syntax to pass the arguments by value or by reference is the same
Does it make sense?

If I only include two, then it just gives me "ambiguous call to overloaded function" for those two


Sorry, but if you include two of what?
Last edited on
two prototypes. I fixed the parameters, so it runs now. Thanks for your help.
Topic archived. No new replies allowed.