如何声明内部模板类?

时间:2012-12-21 07:58:55

标签: c++ templates inner-classes

我在内部模板类的声明中面临问题。

我在Class A中创建了a.h,这里的代码看起来像是

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;
}

任何帮助都将受到高度赞赏,

谢谢&amp; BR Yuvi

1 个答案:

答案 0 :(得分:3)

把它移到里面:

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;
      };
};

在定义时添加A的范围,如下所示:

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