From: Daniel on
How do I pass a variable of type std::vector in an argument list to a
function?

Daniel


From: Igor Tandetnik on
"Daniel" <Mahonri(a)cableone.net> wrote in message
news:OokUt7NpIHA.2064(a)TK2MSFTNGP05.phx.gbl
> How do I pass a variable of type std::vector in an argument list to a
> function?

The same way you pass a value of any other type. What exactly seems to
be the problem?

Note that std::vector is not a type, it's a template. std::vector<int>
and std::vector<SomeClass> are types.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: Daniel on
I get an error when I try to define this function:

void print_array(std::vector v);

error C2955: 'std::vector' : use of class template requires template
argument list

"Igor Tandetnik" <itandetnik(a)mvps.org> wrote in message
news:uiCnh%23NpIHA.3428(a)TK2MSFTNGP02.phx.gbl...
> "Daniel" <Mahonri(a)cableone.net> wrote in message
> news:OokUt7NpIHA.2064(a)TK2MSFTNGP05.phx.gbl
>> How do I pass a variable of type std::vector in an argument list to a
>> function?
>
> The same way you pass a value of any other type. What exactly seems to be
> the problem?
>
> Note that std::vector is not a type, it's a template. std::vector<int> and
> std::vector<SomeClass> are types.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>


From: Igor Tandetnik on
"Daniel" <Mahonri(a)cableone.net> wrote in message
news:uAYEWSOpIHA.524(a)TK2MSFTNGP05.phx.gbl
> I get an error when I try to define this function:
>
> void print_array(std::vector v);

Like I said, there's no such type as std::vector. You need to provide
the template parameter (it's also better to pass by reference):

void print_array(const std::vector<int>& v);

Alternatively, you could make print_array itself a template:

template <typename T>
void print_array(const std::vector<T>& v);

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: Anders Karlsson on
On Tue, 22 Apr 2008 22:17:29 -0400, "Igor Tandetnik"
<itandetnik(a)mvps.org> wrote:

> "Daniel" <Mahonri(a)cableone.net> wrote in message
> news:uAYEWSOpIHA.524(a)TK2MSFTNGP05.phx.gbl
> > I get an error when I try to define this function:
> >
> > void print_array(std::vector v);
>
> Like I said, there's no such type as std::vector. You need to provide
> the template parameter (it's also better to pass by reference):
>
> void print_array(const std::vector<int>& v);
>
> Alternatively, you could make print_array itself a template:
>
> template <typename T>
> void print_array(const std::vector<T>& v);

another common way (and prefered) is to declare a typedef of the
vector, maybe then it's clearer?

typedef std::vector<int> int_vector;

void print_array( int_vector v );

or more effectively (and showing also that the vector is not modified
in the function which the function name already implies):

void print_array( const int_vector &v )

hth/Anders.
--
A: People bitching about top-posting

> Q: What's the most annoying thing on USENET?