C++方法声明与 这是一个简单的二叉树C++类。编译器抛出一个错误:
E0147声明与void BinaryTree::getLeftChildnode*n不兼容 这里节点是在类的private部分下定义的结构。我不知道为什么它说不相容的声明C++方法声明与 这是一个简单的二叉树C++类。编译器抛出一个错误:,c++,class,struct,private,C++,Class,Struct,Private,E0147声明与void BinaryTree::getLeftChildnode*n不兼容 这里节点是在类的private部分下定义的结构。我不知道为什么它说不相容的声明 //------------------------ BinaryTree class----------------- class BinaryTree { public: BinaryTree(); ~BinaryTree(); void createRootNode(); void ge
//------------------------ 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)
{
}
带错误的代码图片
第一个错误是,您需要向前声明节点。 第二个错误是,您正在尝试访问在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定义为private,那么也需要在类内部实现
第二个答复:
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;
};
我在全局范围内得到了另一个结构,它被声明为node,这造成了混乱。其次,我还需要确定公共和私人部门的顺序 这是工作代码
//------------------------ 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)
{
}
好奇的是,如果节点是私有的,那么外部函数将如何使用您的getLeftChildnode*n?在这个BinaryTree类之前,您是否在某处有类节点或结构节点?@Peter void BinaryTree::getLeftChildnode*n的函数定义与OP编写的void BinaryTree::getLeftChildBinaryTree::node*n完全相同;编译器知道我们在BinaryTree中,因为BinaryTree::。问题是函数声明引用了一个节点*,但在此之前没有声明该节点,我假设它被声明为一种不同的、不相关的类型。在我看来,您应该能够将所有公共内容移到私有内容之下,并及时加入批萨的行列。二叉树只有两个子注释,右边和左边,所以我不太明白队列给表带来了什么。将结构节点声明留在BinaryTree类中是有意义的,只需将其设置为公共而不是私有,并将其放置在方法声明之上,以便它们知道它是什么:类BinaryTree{public:typedef struct node{…}节点;…void getLeftChildnode*n;void getRightChildnode*n;…};你知道在你的答案发布后你会编辑它,对吗?我已经更新了我的答案。我希望现在能令人满意。第二种选择不起作用。它向前声明Node,而不是BinaryTree::Node。这使得节点未定义,在实现成员函数之前,您可能看不到这一点。您对选项1的建议将起作用,但它忽略了最简单的解决方案:对类中的声明进行重新排序,使节点在第一次使用之前出现。