【发布时间】: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带来了什么。