call a function with vector attributes

Hello, I want to call a function with vector attributes. I get an error concerning the declaration of the vector. how could I do it
Probably you're not defining the type of the vector you want to pass in the function. You can do something like this:

1
2
3
4
void pass_vector(vector<int> v_int)
{
    // code
}


In previous example i passed a specific type to vector, which is int, but if you want pass a type in parameter you need to create a template function:

1
2
3
4
5
template <typename T>
void pass_vector(vector<T> v)
{
    //code here
}



Help: http://www.cplusplus.com/doc/tutorial/templates.html

1
2
3
4
5
template <typename T>
void pass_vector(vector<T>& v, const vector<T>& z)
{
    //code here
}


Now vector v is changeable while z is not (passed by reference and by value respectively). You need to have the &, you cannot pass the whole vector, the & passes the address.
Be sure that you are using namespace std; other wise you have to use std::vector<T>.
Last edited on
Topic archived. No new replies allowed.