Why this won't work

My iterator doesn't work I don't know why.
1
2
3
4
5
6
7
8
template <class T>
T loopthrough( T element){
typedef std::string::std::iterator q;
q = element.begin();
for(q = element.begin(); q != element.end(); q++){
    std::cout << *q << std::endl;
}
}
Does not compile? Does not do what you expect it to do? Can you be more specific?
It doesn't compile.
Can you be more specific?

Compiler errors? Handing us a snippet of code out of context and just saying it does not compile is vague. Please copy paste the error messages that your compiler gives you.
q was not declared in this scope
'std' in 'std::string {aka class std::basic_string <char}' does not name a type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template < typename SEQUENCE > void print( const SEQUENCE& seq ) {

    // using iterator = decltype( std::begin(seq) ) ; // type alias for the type of iterator
    // auto is simpler
    for( auto iter = std::begin(seq) ; iter != std::end(seq) ; ++iter ) {
        std::cout << *iter << ' ' ;
    }
    std::cout << '\n' ;
}

template < typename SEQUENCE > void easy_print( const SEQUENCE& seq ) {

    for( const auto& value : seq ) std::cout << value << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/f571408ab1d4fa58
Ah, I can see now. You declared q as a typedef.
Change line 3/4:

std::string::iterator q = element.begin();
Last edited on
Topic archived. No new replies allowed.