Passing vectors into a function
Apr 15, 2010 at 2:39am UTC
Here's what I have so far:
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
int save(int &x, int &y, string filename, vector<string> *); //function declaration
int main ()
{
vector<string> inventory;
save(x_coord, y_coord, file_name, inventory);
}
int save(int &x, int &y, string filename, vector<string> invec)
{
ofstream out_stream;
out_stream.open(filename.c_str());
if ( out_stream.fail())
return -1;
out_stream << x << "," << y << endl;
for (unsigned int i = 0; i < invec.size( ); i++)
{
out_stream << invec[i] << endl;
}
out_stream.close();
if (!( out_stream.fail()))
return 0;
}
The error I'm getting is: undefined reference to `save(int&, int&, std::string, std::vector<std::string, std::allocator<std::string> >*)'
Anyone know what I'm missing? (I'm only worried about the vector, I know I didn't include the declaration of x_coord, y_coord, and file_name).
Thanks!!
Apr 15, 2010 at 3:54am UTC
The declaration indicates a vector pointer but you are passing the object in the call. You should be passing by reference anyway.
int save(int &x, int &y, const string& filename, const vector<string>& invec);
Topic archived. No new replies allowed.