Yuvi
Yuvi

Reputation: 1350

How to declare inner template class?

I am facing issue in declaration of inner template class.

I have create a Class A in a.h here is my code looks like

class A
{
  public:

  private:
    // How declare LockFreeQueue here

};

template <typename T>
struct LockFreeQueue
{
      LockFreeQueue();
      void push(const T& t);
      bool pop(T& t);
    private:
      typedef std::list<T> TList;
      TList list;
      typename TList::iterator iHead, iTail;

};

/**
 * Constructor
 * Initializes required variables
 * */
template <typename T>
LockFreeQueue<T>::LockFreeQueue()
{
  list.push_back(T());
  iHead = list.begin();
  iTail = list.end();
}

/**
 * pushes data in the list
 * @param datatype that needs to be pushed
 * */
template <typename T>
void LockFreeQueue<T>::push(const T& t)
 {
   list.push_back(t);
   iTail = list.end();
   list.erase(list.begin(), iHead);
 }

/**
 * Pops Queue
 * @param t stores popped data at t's address
 * @return true if data available, false otherwise
 * */
template <typename T>
bool LockFreeQueue<T>::pop(T& t)
{
  typename TList::iterator iNext = iHead;
  ++iNext;
  if (iNext != iTail)
  {
    iHead = iNext;
    t = *iHead;
    return true;
  }
  return false;
}

Any help would be highly appreciated,

Thanks & BR Yuvi

Upvotes: 0

Views: 1446

Answers (1)

SomeWittyUsername
SomeWittyUsername

Reputation: 18358

Just move it inside:

class A
{
  public:

  private:

      template <typename T>
      struct LockFreeQueue
      {
          LockFreeQueue();
          void push(const T& t);
          bool pop(T& t);
         private:
          typedef std::list<T> TList;
          TList list;
          typename TList::iterator iHead, iTail;
      };
};

And add the scope of A when defining, like this:

template <typename T>
A::LockFreeQueue<T>::LockFreeQueue()

Upvotes: 3

Related Questions