c++ - C++ 方法声明不兼容
问题描述
这是一个用于二叉树的简单 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)
{
}
有错误的代码图片
解决方案
我在全局范围内得到了另一个结构,声明为“节点”,这造成了混乱。其次,我还需要修复公共和私人部分的顺序。
这是工作代码
//------------------------ 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)
{
}
推荐阅读
- vue.js - VueJS 可重用组件未更新第二个组件的数据
- windows-7 - IE11 的页面设置中的页面大小选项在 Windows 7 中仅显示 Oficio 216x340mm、Oficio 8.5x13 和 Youkei 3 (98 x 148mm)
- c# - 将 Int 数组传递给 LINQ 运算符以获取特定对象
- python - 可以计算不同产品价值、折扣等的 Python 代码,供商店客户使用
- c# - 如何使用在kafka中发送的最新文本文件数据并使用c#写回另一个文本文件?
- c# - ASP.NET 中的 MVC - 使用 IEnumerable 显示值而不是 Id
- html - 解码 Html 实体
- python - 有没有办法检查 PyQt5 窗口是否打开并相应地延迟另一个实例?
- xml - 是否可以为 VS Code 中的某些 XML 标签分配不同的颜色?
- ios - 使用 url 方案将 .ovpn 配置文件从 iOS 应用程序发送到官方 openVPN 客户端