Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/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_Queue - Fatal编程技术网

C++ 模板出列方法具有未声明的标识符

C++ 模板出列方法具有未声明的标识符,c++,templates,queue,C++,Templates,Queue,因此,我目前正在实现一个作为单链表的队列。一切都进行得很顺利,但编译器正在标记我的出列方法 这就是Visual Studio对我咆哮的内容: 错误C2065:“已删除”:未声明的标识符 下面是我的出列方法,它应该返回刚从队列中删除的值: template <typename Type> Type QueueLinked<Type>::deque() { if (queueFront == 0) { cout << "Queue is empty! Th

因此,我目前正在实现一个作为单链表的队列。一切都进行得很顺利,但编译器正在标记我的出列方法

这就是Visual Studio对我咆哮的内容:

错误C2065:“已删除”:未声明的标识符

下面是我的出列方法,它应该返回刚从队列中删除的值:

template <typename Type>
Type QueueLinked<Type>::deque() {
if (queueFront == 0) {
    cout << "Queue is empty! There's nothing to remove!" << endl;
} else {
    nodeType<Type> *temp;
    temp = queueFront;
    queueFront = queueFront->next;
    Type Removed = temp->dataItem;
    delete temp;

    if (queueFront == 0) {
        queueRear = 0;
    }
}
return Removed;
} 
模板
类型QueueLinked::deque(){
if(queueFront==0){
cout数据项;
删除临时文件;
if(queueFront==0){
队列后部=0;
}
}
移除返回;
} 
这是我的节点结构:

template <typename Type>
struct nodeType {
    Type dataItem;
    nodeType<Type> *next;
};
模板
结构节点类型{
类型数据项;
nodeType*下一步;
};

这似乎是一个非常简单的错误,但我不知道是什么原因造成的。希望我不是太笨,但这不是第一次了

您可以在
else
块中声明它,当然它在它之外是未声明的。在
if
之前声明它

试着这样做:

template <typename Type>
Type QueueLinked<Type>::deque() {
  Type Removed;
  if (queueFront == 0) {
    cout << "Queue is empty! There's nothing to remove!" << endl;
  } else {
    nodeType<Type> *temp;
    temp = queueFront;
    queueFront = queueFront->next;
    Removed = temp->dataItem;
    delete temp;

    if (queueFront == 0) {
        queueRear = 0;
    }
  }
return Removed;
} 
模板
类型QueueLinked::deque(){
删除类型;
if(queueFront==0){
cout数据项;
删除临时文件;
if(queueFront==0){
队列后部=0;
}
}
移除返回;
} 

您可以在
else
块中声明它,当然它在它之外是未声明的。在
if
之前声明它

试着这样做:

template <typename Type>
Type QueueLinked<Type>::deque() {
  Type Removed;
  if (queueFront == 0) {
    cout << "Queue is empty! There's nothing to remove!" << endl;
  } else {
    nodeType<Type> *temp;
    temp = queueFront;
    queueFront = queueFront->next;
    Removed = temp->dataItem;
    delete temp;

    if (queueFront == 0) {
        queueRear = 0;
    }
  }
return Removed;
} 
模板
类型QueueLinked::deque(){
删除类型;
if(queueFront==0){
cout数据项;
删除临时文件;
if(queueFront==0){
队列后部=0;
}
}
移除返回;
} 

你完全正确。我应该知道,我只是在某些情况下才返回声明的内容。新手犯了错误,但这是一个教训。你也可以问自己,当什么都没有被移除的时候,你到底返回了什么!我立即被怀疑是那个事实。在我的头脑中,我认为它只是没有定义,但在Matzi的帮助下,很明显,如果它从未被声明,那么这将导致一个完全不同的问题。你是100%正确的。我应该知道,我只是在某些情况下才返回声明的内容。新手犯了错误,但这是一个教训。你也可以问自己,当什么都没有被移除的时候,你到底返回了什么!我立即被怀疑是那个事实。在我的头脑中,我认为它只是没有定义,但在马特齐的帮助下,很明显,如果它从未被宣布,那么这将导致一个完全不同的问题。