【问题标题】:C++ method declaration is incompatible withC++ 方法声明不兼容
【发布时间】:2019-09-20 22:58:55
【问题描述】:

这是一个用于二叉树的简单 c++ 类。编译器抛出错误:

E0147 声明与“void BinaryTree::getLeftChild(node *n)”不兼容

这里的node 是在类的private 部分下定义的结构。我不知道为什么它说不兼容的声明。

//------------------------ BinaryTree class-----------------
class BinaryTree
{
public:
    BinaryTree();
    ~BinaryTree();
    void createRootNode();
    void getChildren();
    void getLeftChild(node* n);
    void getRightChild(node* n);

private:
    typedef struct node
    {
        node *lchild = nullptr;
        int data;
        node *rchild = nullptr;
    }node;

    queue <node*> Q;
    node *root; 
};

BinaryTree::BinaryTree()
{
    createRootNode();
    getChildren();
}

void BinaryTree::createRootNode()
{
    root = new node();
    cout << "Enter value for root node" << endl;
    cin >> root->data;
    Q.push(root);
}

void BinaryTree::getChildren()
{
    while (Q.empty == false)
    {
        getLeftChild(Q.front());
        getRightChild(Q.front());
        Q.pop();
    }
}

void BinaryTree::getLeftChild(node* n)
{

}

void BinaryTree::getRightChild(node* n)
{

}

有错误的代码图片

【问题讨论】:

  • 只是好奇,但是如果node 是私有的,外部函数将如何使用您的getLeftChild(node* n)?
  • 在这个BinaryTree 课程之前,你有没有class node 或struct node 某处?
  • @Peter void BinaryTree::getLeftChild(node* n) 的函数定义完全等同于如果 OP 写了void BinaryTree::getLeftChild(BinaryTree::node* n);由于BinaryTree::,编译器知道我们在“内部”BinaryTree。问题是函数声明引用了node*,但在此之前没有声明node(我假设它被声明为不同的、不相关的类型)。
  • 在我看来,您应该能够将所有 public 内容移至 private 内容下方,并及时完成以加入披萨团伙。
  • 二叉树只有左右两个子音符,所以我不太明白queue 带来了什么。

标签: c++ class struct private


【解决方案1】:

我在全局范围内获得了另一个声明为“节点”的结构,这造成了混乱。其次,我还需要修复公共和私人部分的顺序。

这是工作代码

    //------------------------ BinaryTree class-----------------
class BinaryTree
{
private:
    typedef struct node
    {
        node *lchild = nullptr;
        int data;
        node *rchild = nullptr;
    }node;

    queue <node*> Q;
    node *root;

public:
    BinaryTree();
    ~BinaryTree();
    void createRootNode();
    void getChildren();
    void getLeftChild(node* n);
    void getRightChild(node* n);
};

BinaryTree::BinaryTree()
{
    createRootNode();
    getChildren();
}

void BinaryTree::createRootNode()
{
    root = new node();
    cout << "Enter value for root node" << endl;
    cin >> root->data;
    Q.push(root);
}

void BinaryTree::getChildren()
{
    while (Q.empty() == false)
    {
        getLeftChild(Q.front());
        getRightChild(Q.front());
        Q.pop();
    }
}

void BinaryTree::getLeftChild(node* n)
{

}

void BinaryTree::getRightChild(node* n)
{

}

【讨论】:

    【解决方案2】:

    第一个错误,是你需要转发声明节点。 第二个错误是您正在尝试访问在 BinaryTree 内部私有声明的节点。

    第一个答案:

    typedef struct node
    {
      node* lchild = nullptr;
      int data;
      node* rchild = nullptr;
    }node;
    
    class BinaryTree
    {
    public:
      BinaryTree();
      ~BinaryTree();
      void createRootNode();
      void getChildren();
      void getLeftChild(node* n);
      void getRightChild(node* n);
    
    private:
      node* root;
    };
    
    void BinaryTree::getLeftChild(node* n)
    {
    
    }
    void BinaryTree::getRightChild(node* n)
    {
    
    }
    

    现在代码可以正常编译了。

    或者,如果您想将 typedef 定义为内部私有,则您也需要实现在类内部。

    第二个答案:

    typedef struct node;
    
    class BinaryTree
    {
    public:
      BinaryTree();
      ~BinaryTree();
      void createRootNode();
      void getChildren();
      void getLeftChild(node* n)
      {
    
      }
      void getRightChild(node* n)
      {
    
      }
    
    private:
      typedef struct node
      {
        node* lchild = nullptr;
        int data;
        node* rchild = nullptr;
      }node;
    
      node* root;
    };
    

    【讨论】:

    • 将struct node 声明留在BinaryTree 类中是有意义的,只需将其设为public 而不是private,并将其放在方法声明上方,以便他们知道它是什么是:class BinaryTree { public: typedef struct node { ... } node; ... void getLeftChild(node* n); void getRightChild(node* n); ...};
    • 你知道你在发布后编辑你的答案,对吧?
    • 我已经更新了我的答案。我希望它现在令人满意。
    • 第二个选项不起作用。它转发声明Node,而不是BinaryTree::Node。这使得Node 未定义,在实现成员函数之前您可能不会看到。您对选项 1 的建议将起作用,但它错过了最简单的解决方案:重新排序类中的声明,以便 Node 在第一次使用之前出现。
    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 2016-01-11
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    相关资源
    最近更新 更多