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_Compiler Errors - Fatal编程技术网

C++ 错误:此声明没有存储或类型说明符

C++ 错误:此声明没有存储或类型说明符,c++,templates,compiler-errors,C++,Templates,Compiler Errors,我从所有具有节点*(此声明没有存储或类型说明符)的内容中获得此消息。有人能帮我一下,请把我送到正确的方向吗 template <typename type> Node* Stack<type>::pop() { Node* retNode; // the node to be return if(tos == NULL) { cerr << "*** Stack empty ***"; exit(1); } else { retNode

我从所有具有
节点*
(此声明没有存储或类型说明符)的内容中获得此消息。有人能帮我一下,请把我送到正确的方向吗

template <typename type>
Node* Stack<type>::pop() {
Node* retNode; // the node to be return
if(tos == NULL) {
    cerr << "*** Stack empty ***";
    exit(1);
}
else {
    retNode = tos; // store the location of tos
    tos = tos->getLink(); // move to new tos
    retNode->setLink(); // unlink the popped node from the stack
    size -= 1;
}
return retNode;
}

Node
是类模板,因此不能将
Node
Node*
用作数据类型。必须在尖括号中添加模板参数,例如
节点
节点*

在您给出的具体示例中,以下内容似乎是合适的:

template <typename type>
Node<type>* Stack<type>::pop() {
  Node<type>* retNode;
  /* ... */
  return retNode;
}
模板
Node*Stack::pop(){
节点*retNode;
/* ... */
返回节点;
}
也就是说,用于
堆栈的同一类型参数也应该(可能)用于
节点

另外两项说明:

  • 奇怪的是,
    节点
    模板似乎实现了堆栈的内部数据结构,而
    节点*
    指针则由堆栈的pop函数返回。返回
    类型
    对象似乎更自然(封装性更好,对堆栈用户来说更直观)

  • 当堆栈为空时,pop函数调用
    exit
    (从而使整个过程停止)似乎也很奇怪。也许返回
    nullptr
    ,或者一个伪对象,或者抛出一个异常(或者类似的策略)会更合适


  • 在编译此代码之前,必须在作用域中声明
    节点
    。那么,它在哪里呢?对不起,我一直在努力理解这个网站在评论部分放置代码的方法,但我仍然遇到问题。不要在评论中发布代码,只需编辑你原来的帖子。非常感谢。我没有意识到这一点,并假设人们希望我们在它下面发表评论。显然,
    节点
    是一个模板,所以你需要使用
    节点*
    而不是
    节点*
    (假设要使用的
    类型与你在
    堆栈
    模板中使用的类型相同)。它似乎工作正常,错误消失了。非常感谢您抽出时间。我真的很感激!
    template <typename type>
    Node<type>* Stack<type>::pop() {
      Node<type>* retNode;
      /* ... */
      return retNode;
    }