Pergunta

I am trying to make an interface that encapsulates the MinPriorityQueue and the MaxPriorityQueue. Both has an iterator and const_iterator (that is why I need an own priority queue instead of in the standard library that doesn't allow iterative access).

template <typename T> class PriorityQueueInterface
{
public:
   typename ArrayList<T>::iterator iterator;
   typename ArrayList<T>::const_iterator const_iterator;

   virtual void insert( T item ) = 0;
   virtual bool get( T key, T& value ) const = 0;
   virtual T front() const = 0;
   virtual T back() const = 0;
   virtual ~PriorityQueueInterface() {}
};


ArrayList is my own creation.

This gives an error:
Error 1 error C2143: syntax error : missing ';' before '<'
I don't understand, because it works if I don't define those typenames. Typenames work if the class is not abstract, but I want the interface to contain those iterators.

Edit: Ok, I think I found the problem, it is unrelated to this code. I recreated the original post, I hope someone will still find it useful.

Foi útil?

Solução 3

You need to use typedef typename instead of typename.

The way you wrote the code, you are telling the compiler that the PriorityQueueInterface has a member called iterator and a member called const_iterator.

By using typedef typename you will tell it it has a type called iterator and a type called const_iterator.

Outras dicas

You need typedef, not typename, to make a type alias. And you also need typename since it's a dependent type:

typedef typename ArrayList<T>::iterator iterator;
^^^^^^^

Your code declares iterator and const_iterator to be data members, not types.

You should use typedef typename construction:

template <typename T> class PriorityQueueInterface
{
public:
    typedef typename ArrayList<T>::iterator iterator;
    typedef typename ArrayList<T>::const_iterator const_iterator;

    virtual void insert( T item ) = 0;
    virtual bool get( T key, T& value ) const = 0;
    virtual T front() const = 0;
    virtual T back() const = 0;
    virtual ~PriorityQueueInterface() {}
};

typename is a (required in this case) hint to the compiler: the next identifier is a type name. typedef used for define new type.

Change typename to typedef.

template <typename T> class PriorityQueueInterface
{
public:
   typedef ArrayList<T>::iterator iterator;
   typedef ArrayList<T>::const_iterator const_iterator;

   virtual void insert( T item ) = 0;
   virtual bool get( T key, T& value ) const = 0;
   virtual T front() const = 0;
   virtual T back() const = 0;
   virtual ~PriorityQueueInterface() {}
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top