函数声明中有什么问题?

What is wrong in the function declaration?

本文关键字:问题 什么 声明 函数      更新时间:2023-10-16

请给我解释一下,在这个方法的声明/描述中有什么错误?

class Set
{
    struct Node {
        // ...
    };
    // ...
    Node* &_getLink(const Node *const&, int) const;
    // ...
};
Node* &Set::_getLink(const Node *const &root, int t) const
{
    // ...
}

我没有看到错误,但是编译器(MS VS c++)给出了许多语法错误。

您忘记完全限定Node的名称(它在Set的作用域中定义):

    Set::Node* &Set::_getLink(const Node *const &root, int t) const
//  ^^^^^

如果没有完全限定,编译器将寻找一个名为Node的全局类型,该类型不存在。

这是一个范围问题。您需要在这里添加Node前缀:

Set::Node* &Set::_getLink(const Node *const &root, int t) const
{
    // ...
}

确实,Node在遇到它的时候是未知的(您是在名称空间范围内,而不是在Set的范围内)。您也可以使用auto:

auto Set::_getLink(const Node *const &root, int t) const -> Node *&
{
    // ...
}

->之后,您进入Set的范围,并且知道Node

你没有在全局作用域中定义Node
所以使用这个代码

//by Set::Node we give compiler that this function exist in class Node
Set::Node* &Set::_getLink(const Node *const &root, int t) const
{
   // ...
}