c++ - 为什么在作为值返回时在类内部定义结构需要范围解析?

标签 c++ class scope

我有一个单向链表实现,如下所示:

标题

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/

相关文章:

c++ - 如何使用 strcmp 将字符串与预定义字符串进行比较

javascript - 无法将动态数据加载到 ui-grid 下拉列表中

c# - 了解 C# 中类的访问说明符

c++ - FILE* 类的分配失败

iphone - 检查 UIAppearance 的可用性

c++ - 什么负责删除我的指针?

c++ - 为什么具有相同名称但不同签名的多重继承函数不被视为重载函数?

c++ - 从 std::cin 读取二进制数据

c++ - 如何拥有 unordered_multimaps 的 unordered_multimap

c++ - 在 block 之间存在间隙的 block 中存在键的情况下使用什么数据结构?