jgashler
jgashler

Reputation: 13

"identifier not found" when trying to declare a list iterator

I am creating a Set class using the standard list container. When I declare the list iterator iter, I get an error:

C3861 'iter':identifier not found

I have found a few examples of other people declaring list iterators in this way, but I may be misunderstanding something about iterators.

#include <list>
#include <iterator>

using namespace std;

template <typename T>
class Set
{
private:
    list<T> the_set;
    list<T>::iterator iter;
public:
    Set() {}
    virtual ~Set() {}

    void insert(const T& item) {
        bool item_found = false;
        for (iter = the_set.begin(); iter != the_set.end(); ++iter) {
            if (*iter == item) item_found = true;
        }
        if (!item_found) {
            iter = the_set.begin();
            while (item > *iter) {
                ++iter;
            }
            the_set.list::insert(iter, item);
        }
    }
}

The error is shown happening at the line:

list<T>::iterator iter;

Upvotes: 1

Views: 372

Answers (1)

Yirkha
Yirkha

Reputation: 13848

The compiler gets confused by that line because it doesn't know what list<T> will be before actually specializing the class with some T.

More formally you would say that list<T>::iterator is a dependent name.

The solution is to add a hint in form of the typename keyword to specify that the construct will refer to some type after all.

I.e. this should help:

    typename list<T>::iterator iter;

Upvotes: 2

Related Questions