[英]Error: this declaration has no storage or type specifier
我收到包含Node*
(此声明没有存储或类型说明符)的所有消息。 有人可以帮忙,请按正确的方向发送给我吗?
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*
但我不知道是什么。
以下是我在堆栈类中使用的节点类的声明。 让我知道您是否也需要对堆栈类的声明,因为我看不到问题。
template <typename type>
class Node<type>{
private:
type data;
Node *link;
public:
Node(type p_item, Node *p_link);
type getData() const;
Node* getLink() const;
void setData(type p_data);
void setLink(Node *node);
};
Node
是一个类模板,因此不能将Node
或Node *
用作数据类型。 您必须在尖括号中添加模板参数,例如Node<int>
或Node<char> *
等。
在您提供的特定示例中,似乎以下情况是适当的:
template <typename type>
Node<type>* Stack<type>::pop() {
Node<type>* retNode;
/* ... */
return retNode;
}
即,用于Stack
的相同类型参数也应该(可能)也用于Node
。
另外两个注意事项:
看起来很奇怪,虽然Node
模板似乎实现了堆栈的内部数据结构,但是Node<type> *
指针是由堆栈的pop函数返回的。 返回type
对象看起来更自然(更好的封装,对堆栈的用户更直观)。
当堆栈为空时,pop函数调用exit
(从而使整个过程停止)似乎也很奇怪。 也许返回nullptr
或虚拟对象,或者抛出异常(或类似策略)会更合适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.