iterators help

Sep 24, 2012 at 10:54am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>
#include <vector>


using namespace std;


template <class T>
T fn(T a,T b)
{
   vector<T>array;
   vector<int>::iterator itr;

    array.push_back(a);
    array.push_back(b);



    sort(array.begin(),array.end());

    for(itr=array.begin();itr<array.end();itr++)
    cout<<*itr;



}

int main()
{

fn<int>(22,3);

}


This source works,but only for integers so when i put this:
vector<T>::iterator itr;
TELL ME error so what need to do?
Sep 24, 2012 at 11:07am
On line 13: change vector<int>::iterator itr; to vector<T>::iterator itr;
Sep 24, 2012 at 11:09am
This is which i told up.
I changed it and tell me ERROR.
SO WHAT NEED TO DO NOW?


(READ MORE carefully.)
Sep 24, 2012 at 11:11am
typename vector<T>::iterator itr;
Sep 24, 2012 at 11:33am
oww,it works,you are fantastic.
Can tell me now why should put typename?
Sep 24, 2012 at 11:43am
I'm still getting an error. What's the deal?
Sep 24, 2012 at 11:51am
In this case typename tells the compiler to use template parameter. Otherwise he wouldn't (some do that nonetheless though).

the error is that fn is declared to return a value but doesn't.

the function can be called so: fn(22,3);
because the compiler has all necessary information to deduce the type.
Sep 24, 2012 at 3:10pm
Can tell me now why should put typename?


Simply put, because it is a nested dependent type. Meaning, the "type" of the iterator is nested inside your template and is dependent on your template type, hence nested dependent.

Topic archived. No new replies allowed.