c++ - 使用模板的嵌套类中的未知类型名称 'class'

标签 c++ templates nested-class

我的嵌套类有问题。该文件使用模板类来实现队列,但有一个我无法理解的编译错误。我个人对该错误的研究表明它与模板值名称的声明有关,但我不确定我做错了什么。

这是该类的源代码:queue.h

#ifndef QUEUE_H
#define QUEUE_H

template<class T>
class Queue
{
private:
    template<class U>
    class Node
    {
    private:
        U _item;
        Node* _next;
    public:
        Node(U item);
        ~Node();
        U getItem();
        Node<U>* getNext();
        void setNext(Node<U>* next);
    };
    unsigned _size;
    Node<T>* _head;
    Node<T>* _tail;

public:
    Queue();
    ~Queue();
    Node<T>* getHead();
    Node<T>* getTail();
    unsigned size();
    bool isEmpty();
    void push_back(T item);
    T pop();
    void combine(Queue<T> qu);
};

//////////////////////////////////
//NODE
//////////////////////////////////
template<class T>
template<class U>
Queue<T>::Node<U>::Node(U item)
//constructor
{
    _item = item;
    _next = NULL;
}

template<class T>
template<class U>
Queue<T>::Node<U>::~Node()
//destructor
{
}

template<class T>
template<class U>
U Queue<T>::Node<U>::getItem()
//returns the provided item.
{
    return _item;
}

template<class T>
template<class U>
Node<U>* Queue<T>::Node<U>::getNext()
//returns a pointer to the next node in the queue.
{
    return _next;
}

template<class T>
template<class U>
void Queue<T>::Node<U>::setNext(Node<U>* next)
//sets the next node in the queue to the provided Node
{
    _next = next;
}
//////////////////////////////////
//QUEUE
//////////////////////////////////
template<class T>
Queue<T>::Queue()
//constructor
{
    _size = 0;
    _head = NULL;
    _tail = NULL;
}

template<class T>
Queue<T>::~Queue()
//destructor
{
    while(!isEmpty()){
        pop();
    }
}

template<class T>
Node<T>* Queue<T>::getHead()
//returns the node at the front of the queue
{
    return _head;
}

template<class T>
Node<T>* Queue<T>::getTail()
//returns the node at the end of the queue
{
    return _tail;
}

template<class T>
unsigned Queue<T>::size()
//returns the size of the queue
{
    return _size;
}

template<class T>
bool Queue<T>::isEmpty()
//if the queue is empty return true, otherwise return false
{
    if(_size == 0){
        return true;
    }
    return false;
}

template<class T>
void Queue<T>::push_back(<T> item)
//add a node to the back of the queue with the provided data item
{
    Node<T>* nnode = new Node<T>(item);
    if(_head == NULL){
        _head = nnode;
        _tail = nnode;
    }
    _tail.setNext(nnode);
    _tail = nnode;
    _size++;
}

template<class T>
T Queue<T>::pop()
//removes the value at the front of the queue and returns it
{
        Node<T>* cur = _head;
        _head = cur->getNext();
        _size--;
        <T> item = cur.getItem();
        delete cur;
        return item;
}

template<class T>
void Queue<T>::combine(Queue<T> qu)
//adds the provided queue to the end of the current queue
{
    _tail.setNext(qu.getHead());
    _tail = qu.getTail();
    _size+= qu.size();
}

#endif

我收到的错误是:

./queue.h:63:1: error: unknown type name 'Node'
Node<U>* Queue<T>::Node<U>::getNext()
^
./queue.h:63:5: error: expected unqualified-id
Node<U>* Queue<T>::Node<U>::getNext()
    ^

如果您有任何建议,我很乐意听取。

最佳答案

改变:

Node<U>* Queue<T>::Node<U>::getNext()

进入:

typename Queue<T>::template Node<U>* Queue<T>::Node<U>::getNext()

还有其他人:

Node<T>* Queue<T>::getHead()

进入:

typename Queue<T>::template Node<T>* Queue<T>::getHead()

还有这个奇怪:

void Queue<T>::push_back(<T> item)

进入:

void Queue<T>::push_back(T item)

最后但同样重要的是:

T item = cur.getItem();

进入:

T item = cur->getItem();

如果在尾随返回类型中指定嵌套类型,则它们不需要作用域,因此:

typename Queue<T>::template Node<U>* Queue<T>::Node<U>::getNext()

你可以说:

auto Queue<T>::Node<U>::getNext() -> Node<U>*

只需说 auto 并将您的原始声明缩短一半:

auto Queue<T>::Node<U>::getNext()

关于c++ - 使用模板的嵌套类中的未知类型名称 'class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25964187/

相关文章:

C++ Lua 错误处理

c++ - Qt如何隐藏横条

c++ - 将字符串转换为 time_t,然后将 time_t 转换回字符串

c++ - 指定一个可以绑定(bind)到函数模板的模板参数

c++ - 模板类构造函数问题——为 multidim 数组设计容器

c++ - 转发声明模板别名

java - 静态嵌套类可以实例化多次吗?

C++ regex_search 捕获检索在运行时失败

Ruby:类中的嵌套类与模块中的嵌套类相同吗?

c++ - friend 类如何访问嵌套类的私有(private)成员?