Author Topic: The joy of templetes  (Read 3373 times)

Gametaku

  • .
  • Founders
  • Posts: 168
  • Karma: +13/-4
  • .
The joy of templetes
« on: February 08, 2007, 03:17:52 PM »
If I define a function such as the following, the compiler has no problems with it.

Code: [Select]
void
findMin ( vector <int> vec, vector<int>::iterator first, vector <int>::iterator last)
{
  vector <int>::iterator  posSmall = min_element (first, last);
  cout <<"Smallest element is " <<vec[*posSmall] << endl;
}

however,  as that can only handle integers,  from what I understand could create a template like the following.


Code: [Select]

 template <typename T>
void
findMin ( vector <T> vec, vector<T>::iterator first, vector <T>::iterator last)
{
  vector <T>::iterator  posSmall = min_element (first, last);
  cout <<"Smallest element is " <<vec[*posSmall] << endl;
 }

The comiler however gives me errors such as
error: class std::vector<T, std::allocator<_CharT> >::iterator' is not a type

charlie

  • Jackass In Charge
  • Posts: 7903
  • Karma: +84/-53
Re: The joy of templetes
« Reply #1 on: February 08, 2007, 03:56:08 PM »
Add typename before vector<T>::iterator to indicate that it is a type.