简体   繁体   English

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

[英]Error: this declaration has no storage or type specifier

I am getting this message with everything that has Node* (this declaration has no storage or type specifier). 我收到包含Node* (此声明没有存储或类型说明符)的所有消息。 Could somebody help and please send me in the right direction? 有人可以帮忙,请按正确的方向发送给我吗?

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;
}

I am sure it's dealing with Node* but I just can't figure out what. 我确定它正在处理Node*但我不知道是什么。

Below are my declarations for the node class that are being used in my stack class. 以下是我在堆栈类中使用的节点类的声明。 Let me know if you need my declarations for the stack class as well because I just cant see the problem. 让我知道您是否也需要对堆栈类的声明,因为我看不到问题。

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 is a class template, so you cannot use Node or Node * as data types. Node是一个类模板,因此不能将NodeNode *用作数据类型。 You must add template arguments in angle brackets, eg Node<int> or Node<char> * etc. 您必须在尖括号中添加模板参数,例如Node<int>Node<char> *等。

In the specific example you gave, it seems the following would be appropriate: 在您提供的特定示例中,似乎以下情况是适当的:

template <typename type>
Node<type>* Stack<type>::pop() {
  Node<type>* retNode;
  /* ... */
  return retNode;
}

Ie the same type argument that is used for Stack should (probably) be used for Node as well. 即,用于Stack的相同类型参数也应该(可能)也用于Node

Two further notes: 另外两个注意事项:

  1. It seems odd that, while the Node template appears to implement internal data structures of your stack, Node<type> * pointers are returned by the pop function of the stack. 看起来很奇怪,虽然Node模板似乎实现了堆栈的内部数据结构,但是Node<type> *指针是由堆栈的pop函数返回的。 It would seem more natural (and better encapsulation, and more intuitive for the users of your stack) to return type objects. 返回type对象看起来更自然(更好的封装,对堆栈的用户更直观)。

  2. It also seems odd that the pop function calls exit (and thus brings the entire process to a halt) when the stack is empty. 当堆栈为空时,pop函数调用exit (从而使整个过程停止)似乎也很奇怪。 Perhaps returning nullptr , or a dummy object, or throwing an exception (or a similar strategy) would be more appropriate. 也许返回nullptr或虚拟对象,或者抛出异常(或类似策略)会更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM