Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未知类型名称';类别';在使用模板的嵌套类中_C++_Templates_Nested Class_C++11_C++14 - Fatal编程技术网

C++ 未知类型名称';类别';在使用模板的嵌套类中

C++ 未知类型名称';类别';在使用模板的嵌套类中,c++,templates,nested-class,c++11,c++14,C++,Templates,Nested Class,C++11,C++14,我的嵌套类有问题。该文件使用模板类实现队列,但有一个我无法理解的编译错误。我个人对该错误的研究表明,它与模板valuename的声明有关,但我不确定我做错了什么 下面是类的源代码:queue.h #ifndef QUEUE_H #define QUEUE_H template<class T> class Queue { private: template<class U> class Node { private: U

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

下面是类的源代码: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
\ifndef队列
#定义队列
模板
类队列
{
私人:
模板
类节点
{
私人:
U_项目;
节点*\u下一步;
公众:
节点(U项);
~Node();
U getItem();
节点*getNext();
void setNext(节点*next);
};
未签名的_大小;
节点*头;
节点*_尾;
公众:
队列();
~Queue();
节点*getHead();
节点*getTail();
无符号大小();
bool是空的();
无效推回(T项);
T pop();
无效联合收割机(队列qu);
};
//////////////////////////////////
//节点
//////////////////////////////////
模板
模板
队列::节点::节点(U项)
//建造师
{
_项目=项目;
_next=NULL;
}
模板
模板
队列::节点::~Node()
//析构函数
{
}
模板
模板
U队列::节点::getItem()
//返回提供的项。
{
退货项目;
}
模板
模板
节点*队列::节点::getNext()
//返回指向队列中下一个节点的指针。
{
返回下一步;
}
模板
模板
无效队列::节点::设置下一步(节点*下一步)
//将队列中的下一个节点设置为提供的节点
{
_下一个=下一个;
}
//////////////////////////////////
//排队
//////////////////////////////////
模板
队列::队列()
//建造师
{
_尺寸=0;
_head=NULL;
_tail=NULL;
}
模板
队列::~Queue()
//析构函数
{
而(!isEmpty()){
pop();
}
}
模板
节点*队列::getHead()
//返回队列前面的节点
{
返回头;
}
模板
节点*队列::getTail()
//返回队列末尾的节点
{
返回尾;
}
模板
未签名队列::大小()
//返回队列的大小
{
返回大小;
}
模板
bool队列::isEmpty()
//如果队列为空,则返回true,否则返回false
{
如果(_size==0){
返回true;
}
返回false;
}
模板
无效队列::推回(项目)
//使用提供的数据项将节点添加到队列的后面
{
节点*nnode=新节点(项目);
如果(_head==NULL){
_头=n节点;
_尾=nnode;
}
_tail.setNext(nnode);
_尾=nnode;
_大小++;
}
模板
T队列::pop()
//移除队列前面的值并返回它
{
节点*cur=_头;
_head=cur->getNext();
_大小--;
item=cur.getItem();
删除cur;
退货项目;
}
模板
无效队列::合并(队列qu)
//将提供的队列添加到当前队列的末尾
{
_tail.setNext(qu.getHead());
_tail=qu.getTail();
_尺寸+=质量尺寸();
}
#恩迪夫
我收到的错误是:

./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()
    ^
/queue.h:63:1:错误:未知类型名称“节点”
节点*队列::节点::getNext()
^
./queue.h:63:5:错误:应为不合格id
节点*队列::节点::getNext()
^
如果您有任何建议,我很乐意听取。

更改:

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

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

typename Queue<T>::template Node<U>* Queue<T>::Node<U>::getNext()
item
Queue::push_back
的定义中,应该是
T item
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 Queue<T>::Node<U>::getNext()