简体   繁体   中英

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?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM