未找到标识符

时间:2011-11-24 04:10:58

标签: c++ class tree

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

注意:在BinaryTree.cpp文件的addNode方法中找到了几行错误,可以在问题的最后找到。此外,我也无法访问大小的值,所以我认为这是我无法解决的某种奇怪的范围问题。我也不能在addNode函数中使用“this”关键字。

根据我的家庭作业指示,我也不被允许使用结构。

Node.H

#include <iomanip>

using namespace std;

class Node
{

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

};

Node.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;
}

BinaryTree.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);

};

BinaryTree.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

2 个答案:

答案 0 :(得分:3)

void addNode(int data)

应该是:

void Tree::addNode(int data)

因为它是类Tree

的成员函数

答案 1 :(得分:1)

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

应该是:

//Adds a node to the this Tree
void Tree::addNode(int data)
相关问题