Re: passing vector as argument

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Tue, 22 Apr 2008 22:17:29 -0400, "Igor Tandetnik"
<itandetnik@xxxxxxxx> wrote:

"Daniel" <Mahonri@xxxxxxxxxxxx> wrote in message
news:uAYEWSOpIHA.524@xxxxxxxxxxxxxxxxxxxx
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?
.



Relevant Pages

  • Isolation testing, need suggestion
    ... with explicit template instantiation and specification. ... ImageDB class responsible for reading / writing images to database. ... void deallocate(); ... template< typename Diagnostics> ...
    (comp.lang.cpp)
  • Help with STL allocators
    ... I have a written a custom allocator for STL, ... void construct ... template ... creates a reference to that type. ...
    (comp.sys.hp.hpux)
  • Help with STL allocators
    ... I have a written a custom allocator for STL, ... void construct ... template ... creates a reference to that type. ...
    (comp.lang.cpp)
  • Re: Help with STL allocators
    ... My allocator looks like this now: ... template ... >> void deallocate ... >> creates a reference to that type. ...
    (comp.lang.cpp)
  • Help: How many explicit specializations required?
    ... I need to have a template for T ... Passing arrays of T will be resolved into case 2. ... void func ... int main ...
    (comp.lang.cpp)