如何正确编写指针函数声明?

How to correctly write a pointer function declaration?

我对如何正确声明指针函数感到很困惑。任何人都可以告诉我正确的方法是什么并纠正这个问题。请注意,class 声明可以扩展但最好不要删除。

以下是我的代码,它会导致以下错误:

error: 'Node::Node' names the constructor, not the type

#include <iostream>

using namespace std;
class Node {
  public:
      int value;
      Node *right;
      Node *left;

      Node* find (int key);
};

Node::Node* find (int key)
{
    if (value == key)              { return this;}
    else if (value > key && right) { right->find(key); }
    else if (value < key && left)  { left->find(key); }
    else                           { return NULL;}
}

int main()
{
    Node *head  = new Node();
    Node *right = new Node ();
    Node *left  = new Node ();
    head->value  = 5;
    right->value = 3;
    left->value  = 9;

    head->right  = right;
    head->left   = left;

    Node *mm;

    mm = head->find(8);
    cout << mm->value <<endl;}

    return 0;
}

名称 Node 在全局范围内,因此不需要任何限定。成员函数 find 的名称在 class Node 的范围内,因此必须加以限定:

Node* Node::find(int key) {
  //...
}

函数定义应该是这样的

Node* Node::find(int key)

这个问题实际上与函数指针或相关问题无关。只是"How to implement a method outside of a class?"。 您有以下方法:

ReturnType CLASSNAME::METHODNAME(Any Parameters)

因此,要有一个不返回的方法:

void Node::test(){}

返回:

int Node::test(){}

返回一个指针:

int* Node::test(){}

返回 Node:

Node Node::test(){}

返回指向 Node 的指针:

Node* Node::test(){}