Yann
Yann

Reputation: 988

typenamed iterator is not a type

I'm writing a templated class, which involves use of iterators. I've found the many questions about how you need to typename an iterator if you're using it with a template, so I'm wondering why it's still not working.

#pragma once

#include <iterator>
#include <list>
#include <tuple>

template <class T>
class Quack
{
    private:
        std::list<T> data;
        typename std::list<T>::iterator iter;

    public:
        Quack();

        void insert(T dat);
        std::tuple<T, T> poppop();

    private:
        //error: 'iter' does not name a type
        iter binarySearch(T toFind, std::list<T>::iterator min, std::list<T>::iterator max);
};

I also tried typedef typename std::list<T>::iterator iter;, but that throws up the error "std::list::iterator is not a type"

So given that I'm using typename, what is it that I'm doing wrong?

I'm using g++ 4.4.5 with the argument -std=c++0x, if it's relevant.

Upvotes: 0

Views: 1367

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

In the function

iter binarySearch(T toFind, std::list<T>::iterator min, std::list<T>::iterator max);

you specified private data member iter as the return type of the function.

This

typename std::list<T>::iterator iter;

is a definition of private data member iter that has type typename std::list<T>::iterator

If you mean a type name instead of the data member then you should to write either

typedef typename std::list<T>::iterator iter;

or

using iter =  typename std::list<T>::iterator;

Also it would be better to follow the genaral convetion used in the C++ Standard and name the type like

typedef typename std::list<T>::iterator iterator;

In this case the function declaration will look like

iterator binarySearch(T toFind, iterator min, iterator max);

Also it is better when iterators are preceed the value that is when the function is declared like

iterator binarySearch( iterator min, iterator max, T toFind );

Upvotes: 2

jrok
jrok

Reputation: 55395

You want a typedef there (right now you're declaring an object named iter):

typedef typename std::list<T>::iterator iter;

Also, you need typename in the declaration of binarySearch:

 iter binarySearch(T toFind, typename std::list<T>::iterator min, typename std::list<T>::iterator max);

Of course, you can use iter at this point:

iter binarySearch(T toFind, iter min, iter max);

Upvotes: 8

Related Questions