function template

Hi, I'm trying to write a function template in C++, and when I compile I get a strange (to me) error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

using namespace std;

template<class T>
T boring(vector<T> S)
{
  vector<T>::iterator s;
  s = S.begin();
  return *s;
}

int main()
{
  vector<int> V(3);
  cout << boring(V);
  return 0;
}


So boring is supposed to return the first element of a vector. When I compile, I get:

g++ test3.cc
test3.cc: In function ‘T boring(std::vector<T, std::allocator<_CharT> >)’:
test3.cc:9: error: expected `;' before ‘s’
test3.cc:10: error: ‘s’ was not declared in this scope
test3.cc: In function ‘T boring(std::vector<T, std::allocator<_CharT> >) [with T = int]’:
test3.cc:17: instantiated from here
test3.cc:9: error: dependent-name ‘std::vector<T,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
test3.cc:9: note: say ‘typename std::vector<T,std::allocator<_CharT> >::iterator’ if a type is meant

Any idea what is wrong here? I'm stumped ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class T>
T boring(vector<T> S) {
  typename vector< T >::iterator s;
  s = S.begin();
  return *s;
}

int main()
{
  vector<int> V;
  V.push_back(10);
  cout << boring(V);
  return 0;
}
Thanks!
IOU a beer.
Send it to Wellington, New Zealand. Corona please :D
Topic archived. No new replies allowed.