question about passing a vector into a function

I have a vector that I want to pass into a function. However I keep getting the error message

/tmp/ccxdV4vZ.o: In function `main':
new.cpp:(.text+0xf6): undefined reference to `up(std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: ld returned 1 exit status


The function takes a vector, which contains a 20x20 matrix, and sums four consecutive values up the columns and stores the values in a second vector.

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
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>

using namespace std;

void up(vector<int>, vector<int>);
/*
void upright(vector<int>, vector<int>);
void right(vector<int>, vector<int>);
void downright(vector<int>, vector<int>);
void down(vector<int>, vector<int>);
void downleft(vector<int>, vector<int>);
void left(vector<int>, vector<int>);
void upleft(vector<int>, vector<int>);
*/

int main()
{
	ifstream text;
	vector<int> hold;
	vector<int> keep;
	int kreep;

	text.open("file");
	while(text>>kreep)
	{
		hold.push_back(kreep);		
	}
	text.close();

	up(hold, keep);
return 0;	
}
void up(vector<int> &v, vector<int> &y)
{
	for (int i = 61; i < v.size(); ++i)
	{	
		y.push_back(v[i]+v[i-20]+v[i-40]+v[i-60]);
	
	}
}

I know my error is in how I'm passing the vector, but I can't tell if it's in the function prototype, the way im calling it, or the definition of the function after main.
Pass the vector by reference in the declaration
Last edited on
The prototype doesn't match the definition. If you wish to modify the vector directly from the function you must pass by reference (definition does) or by pointer. The former would be best.

1
2
void up(vector<int>, vector<int>); //should pass reference here
void up(vector<int> &v, vector<int> &y)


Also you could make v a constant since you aren't modifying it directly.
Last edited on
Thanks guys, got it figured out
Topic archived. No new replies allowed.