1

I'm trying to compile code written in C ++.

I have this code in fifo_list.h

template <class T>
class FIFO_LIST
{
  public:
  class list_type {
  public:
    T data;
    list_type *next;
    void *operator new(size_t num_bytes)
    {
       block_pool mem_pool;

       void *mem_addr = mem_pool.pool_alloc( num_bytes );
       return mem_addr;
     } // new
   };  // class list_type
   private:

  list_type *list;

  public:
  /** define a handle type to abstract the list_type type */
  typedef list_type *handle
  handle first(void)
  {
    return list;
   } // first

 }

and this header queue.h:

#include "fifo_list.h"
template <class T>
class queue : protected FIFO_LIST<queueElem<T> *>
{ 
 public:  
  queueElem<T> *queueStart()
  {
    handle h = first();
    queueElem<T> *elem = get_item( h );
    return elem;
   } 
 }

When I try to compile I have these error messages:

include/queue.h: In member function ‘queueElem<T>* queue<T>::queueStart()’:
include/queue.h:100: error: ‘handle’ was not declared in this scope
include/queue.h:100: error: expected ‘;’ before ‘h’
include/queue.h:101: error: ‘h’ was not declared in this scope

where I'm wrong?

@Piotr Skotnicki, @Barry I have modified the code in this way

queueElem<T> *queueStart()
{
  //handle h = first();
  typename FIFO_LIST<queueElem<T> *>::handle h = first();
  queueElem<T> *elem = get_item( h );
  return elem;
 } // queueStart

now I have this errors:

include/queue.h:101: error: there are no arguments to ‘first’ that  depend on a template parameter, so a declaration of ‘first’ must be available
famedoro
  • 1,223
  • 2
  • 17
  • 41
  • Related, but not exactly the same: http://stackoverflow.com/q/4643074/2069064 – Barry Oct 21 '15 at 17:25
  • @Piotr Skotnicki ,thaks for your answer . I have modified the code in thsi way queueElem *queueStart() { //handle h = first(); typename FIFO_LIST *>::handle = first(); queueElem *elem = get_item( h ); return elem; } // queueStart but now I have this errors: include/queue.h: In member function ‘queueElem* queue::queueStart()’: include/queue.h:101: error: expected unqualified-id before ‘=’ token include/queue.h:102: error: ‘h’ was not declared in this scope – famedoro Oct 21 '15 at 17:25
  • I do not get the design: Why the queue a `FIFO_LIST *>` (pointer) ? –  Oct 21 '15 at 17:34

2 Answers2

2

For some reason I cannot find a good duplicate of this...


handle is a dependent name. Unqualified lookup will not find dependent names in base classes, so you will have to qualify it:

typename FIFO_LIST<queueElem<T> *>::handle h = first();

Similarly, since first comes from the base class as well, that needs to be qualified:

typename FIFO_LIST<queueElem<T> *>::handle h = FIFO_LIST<queueElem<T> *>::first();

Though you can shorten the latter qualification by simply using this->:

typename FIFO_LIST<queueElem<T> *>::handle h = this->first();
Barry
  • 286,269
  • 29
  • 621
  • 977
1

This is a known problem of two-stage template instantiation (this is why I do not like it).

To fix your code, use following:

typename FIFO_LIST<queueElem<T> *>::handle h = this->first();
SergeyA
  • 61,605
  • 5
  • 78
  • 137