我有一个单向链表实现,如下所示:
标题
class SinglyLinkedList
{
struct Node
{
Node * _pNext;
int _data;
};
public:
Node * SomeFun(Node * ip1, Node * ip2);
// Some more methods here
};
现在在实现这个类的其中一个方法的时候
CPP
Node * SinglyLinkedList::SomeFun(Node * ip1, Node * ip2)
{
//Some code and return
}
我不理解的奇怪行为是编译时,编译器 拒绝识别返回类型中的类型“节点”,除非我将其指定为 SinglyLinkedList::Node。但是函数参数的相同类型被识别 没有明确指定..理想情况下,我觉得在这两种情况下都应该有 不需要这个显式指定,因为 Node 是在同一个类中定义的。 任何人都可以对此有所了解吗?
最佳答案
SinglyLinkedList::Node * SinglyLinkedList::SomeFun
在这里,您不在类范围内。但是在参数子句中,或者在函数中,你在类范围内,所以你不应该限定 Node
来自 SinglyLinkedList
类,因为编译器已经知道了。
n3376 3.3.7/1
The following rules describe the scope of names declared in classes.
The potential scope of a declaration that extends to or past the end of a class definition also ex- tends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, member func- tion definitions (including the member function body and any portion of the declarator part of such definitions which follows the declarator-id, including a parameter-declaration-clause and any default arguments
关于c++ - 为什么在作为值返回时在类内部定义结构需要范围解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51110599/