C++ vector 作为参数传递给函数问题?
将v作为参数传递给add 实现加一,代码如下:#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
void add(vector<int> v);
int main() {
vector<int> v;
for(int i = 0; i < 15; i++)
v.push_back(rand()%20);
vector<int>::iterator p;
for(p = v.begin(); p != v.end(); p++)
cout << (*p) << ",";
cout << endl;
add(v);
for(vector<int>::iterator p = v.begin(); p != v.end(); p++)
cout << (*p) << ",";
cout << endl;
return 0;
}
void add(vector<int> & B) {
vector<int>::iterator p;
for(p = B.begin(); p != B.end(); p++)
(*p) += 1;
}
编译报如下错误:
/tmp/ccQrbXVR.o: In function `main':
test1.cpp:(.text+0x11c): undefined reference to `add(std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
请帮我看看是哪儿的问题?