Hi, LeafyCircuits here (•‿•)
After recently learning about templates, I began an experiment, but so far, that isn't turning out well.
I'm trying to templatize a custom function that overloads the << operator.
Here's the code:
1 2 3 4 5 6 7 8 9 10
|
template <typename T>
ostream& operator << (ostream &out, vector<T> &vec)
{
vector<T>::iterator itr=vec.begin();
for (;itr!=vec.end();itr++)
{
out << *itr; //edit: was 'cout', now 'out'
}
return out;
}
|
I want to be able to output the contents of any vector by simply calling
cout << somevector << endl;
However, when I try to compile, I get the following errors:
Line 4: expected `;' before "itr"
Line 5: `itr' undeclared (first use this function) |
But, I know for a fact that I have correct syntax on this line:
vector<T>::iterator itr=vec.begin();
So here's my question: Can you templatize an operator this way? If not, can you templatize operators in general?
I'm terribly new to the forums, so constructive criticism is greatly appreciated. Thank you for reading my plight. (•‿•)