0

我在声明内部模板类时遇到问题。

Class Aa.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;
}

任何帮助将不胜感激,

谢谢 & BR Yuvi

4

1 回答 1

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()
于 2012-12-21T08:10:10.487 回答