gpt4 book ai didi

c++ - 找不到标识符

转载 作者:太空狗 更新时间:2023-10-29 21:06:07 32 4
gpt4 key购买 nike

因此,对于我的任务,我应该实现一个仅包含数据和指向其两个兄弟节点的指针的 N​​ode 类,以及一个读取这些节点并从中创建二叉树的 BinaryTree。我的问题是指向树的根似乎不起作用。您可以提供的任何帮助将不胜感激!

注意:错误出现在 BinaryTree.cpp 文件中 addNode 方法的几行中,可以在问题的末尾找到。另外,我也无法访问 size 的值,所以我相信这是我无法解决的某种奇怪的范围问题。我也不能在 addNode 函数中使用“this”关键字。

根据我的作业说明,我也不允许使用结构。

节点.H

#include <iomanip>

using namespace std;

class Node
{

public:
int data;
Node* leftChild;
Node* rightChild;
Node(int data, Node* leftChild, Node* rightChild);

};

节点.cpp

#include <iomanip>
#include <iostream>
#include "Node.h"

using namespace std;

Node::Node(int data, Node* leftChild, Node* rightChild)
{
this->data = data;
this->leftChild = leftChild;
this->rightChild = rightChild;
}

二叉树.H

#include <iomanip>
#include "Node.h"

using namespace std;

class Tree
{

public:
Tree(int data);
void addNode(int data);
void inOrder(Node* N);

protected:
Node* root;
int size;
int data;

private:
int printNode(Node* N);

};

二叉树.cpp

#include <iostream>
#include <iomanip>
#include "BinaryTree.h"

using namespace std;

//Tree constructor. Sets the values of data, size, and root.

Tree::Tree(int data)
{
this->data = data;
this->size = 0;
this->root = new Node(data, NULL, NULL);
}

//Adds a node to the current Tree.
void addNode(int data)
{

Node* tempNode = new Node(data, NULL, NULL);
Node* current = root; //THIS IS THE ERROR LINE.

while(current!=NULL)
{
//If the data we are trying to add is already in the Tree
if(current->data == tempNode->data)
{
cout << "Data already in the Tree.";
}

//If the data for the new node is larger than the old
else if(current->data < tempNode->data)
{
//See if the right child is null. If so, add the tree node there.
if(current->rightChild == NULL)
{
current->rightChild = tempNode;

return;
}

//Otherwise, traverse down the right tree.
else
{
current = current->rightChild;
}
}

//The data is smaller than the current node
else
{
//See if the left child is null. If so, add the tree node there.
if(current->leftChild == NULL)
{
current->leftChild = tempNode;
return;
}

//Otherwise, traverse down the left tree
else
{
current = current->leftChild;
}
}//End of leftChild Else

}//End of while

}//End of addNode

最佳答案

void addNode(int data)

应该是:

void Tree::addNode(int data)

因为它是类 Tree 的成员函数

关于c++ - 找不到标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8252123/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com