user3150201
user3150201

Reputation: 1947

C++ error: 'iterator is not a type'

Here's my code:

template <typename container_type>
void transfer(container_type container, iterator begin, iterator end) {
    for (; begin != end; begin++)
        if (!element_in_container(container, *begin))
            container.insert(iterator, *begin);
}

I get the error 'iterator is not a type'.

I tried adding std:: or container_type:: before iterator, didn't help. I tried defining the template as template <typename container_type<typename T> > and the iterators as container_type<T>::iterator, no luck. What's wrong?

Upvotes: 1

Views: 2939

Answers (2)

Columbo
Columbo

Reputation: 61009

I tried adding std:: or container_type:: before iterator, didn't help.

container_type::iterator is a dependent name, therefore you need the typename keyword before it to treat it as a type (typename container_type::iterator). That is explained in depth here.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

I think you mean the following

template <typename container_type>
void transfer( container_type container, typename container_type::iterator begin, 
                                         typename container_type::iterator end) {

Take into account that in any case your function is wrong because after inserting an element in the container iterators can be invalid.

Upvotes: 6

Related Questions