c++ - 如何为包含结构的类声明成员函数?

标签 c++

我一直在努力编译这段代码,但我似乎无法获得正确的语法。我想让这个函数成为 bst 类的成员函数。 Node 结构和 FindMin 在类外可以很好地相互配合,但我无法在类内进行声明。

//class bst
//{
    public:
    struct Node
    {
        int data;
        struct Node *left;
        struct Node *right;
    };

    //struct FindMin(Node*);

//};

//Function to find minimum in a tree.
Node* FindMin(Node* root)
{
    while(root->left != NULL) root = root->left;
    return root;
}

最佳答案

您可以像声明其他成员函数一样声明成员函数。 Node 在类的范围内声明,因此您可以直接引用它。

如果将定义放在类主体之外,则必须指定返回值的完整范围。

class bst
{
    public:
    struct Node
    {
        int data;
        struct Node *left;
        struct Node *right;
    };

    Node* FindMin(Node*);

};

bst::Node* bst::FindMin(Node* root) {
    // ...  
}

关于c++ - 如何为包含结构的类声明成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49566645/

相关文章:

c++ - 如何在 Linux 集群上构建用于 C++ 的 BLAS 和 LAPACK?

c++ - 使用其他类的 vector 作为反向迭代器

c++ - std::array<T, ?> 类型的成员变量

c++ - (C++) 使用节点/链表时得到不一致的结果。

C++ 将指针从一个派生类转换为另一个派生类

c++ - 尝试使用已删除的函数

c++ - 如果互斥体和数据项在内存中靠近在一起,那么缺点在哪里?

c++ - 通过创建局部变量减少堆栈指针

c++ - 错误 C2280:Class::Class(void):尝试引用已删除的函数

c++ - OpenGL 红皮书示例中的错误,第 3 章