shau-kote
shau-kote

Reputation: 1150

What is wrong in the function declaration?

Explain to me, please, in what a mistake in the declaration/description of this method?

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

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

I don't see mistakes, but the compiler (MS VS C++) gives out many syntax errors.

Upvotes: 1

Views: 178

Answers (3)

Andy Prowl
Andy Prowl

Reputation: 126432

You forgot to fully qualify the name of Node (which is defined in the scope of Set):

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

Without the fully qualification, the compiler will look for a global type named Node, which does not exist.

Upvotes: 3

Mahdi-bagvand
Mahdi-bagvand

Reputation: 1407

you dont define Node in global scope
so use this code

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

Upvotes: 0

Alexandre C.
Alexandre C.

Reputation: 56956

The problem is a scoping one. You need to prefix Node here:

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

Indeed, Node is unknown at the time it is encountered (you are at namespace scope, not inside Set's scope). You can also use auto:

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

After ->, you are at Set's scope and Node is known.

Upvotes: 0

Related Questions