But every time I try to compile, I get something to the effect of
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion)
I had thought that this would have been avoided due to each template type being initialized at compile time, based on what type was passed to the template. Is there anything that I can do to fix this, or should I just go back to overloading types like before?
Read the error. You can't use cout.operator<<() on a vector<>::iterator() object. You need to change the type you're passing to it, and pass that type.
example:
1 2 3 4 5
//we can pass a string, so that's what we'll do
for(vector<string>::const_iterator it = a_vector.begin(); it != a_vector.end(); ++it)
{
print_text(*it); //print the string, not the iterator
}
print_text(it) will pass an iterator, while print_text(*it) will pass a string. You have to dereference the iterator.
This is what I'm not understanding. I know what an iterator is in the context of a vector, but what does this have to do with template functions? I'm not iterating over anything in this code am I?
I know what an iterator is in the context of a vector, but what does this have to do with template functions?
When you feed an iterator to a template function, everything.
I'm not iterating over anything in this code am I?
If you're talking about the code in your first post, no, you're not... But you are dealing with an iterator according to the error message and if you want your template function to work with an iterator, you need to supply the corresponding operator<< overload.
Ahh, just figured it out. I must have been passing it an iterator, which the template function couldnt make sense of when it needed to create the instance of the template function for that type. The code is working fine now, thanks. :)