【问题标题】:I don't know what's wrong with my code - Binary Tree我不知道我的代码有什么问题 - 二叉树
【发布时间】:2013-06-17 15:31:05
【问题描述】:

我现在正在使用朋友类在 C++ 中制作二叉树。

但是,出了点问题,我不知道应该改变什么

template <class Type>
class BinaryTree{
public:

BinaryTree(){
    root = new BTNode<Type>();  
    currentNode = NULL;
}
~BinaryTree(){
    delete root, currentNode;
};

void insertItem(Type data){
    if(currentNode==NULL){
        Item = data;
        currentNode = root;
    }
    if(data<currentNode){
        if(currentNode->Left.is_empty()){
            currentNode->Left = new BTNode(data);
            currentNode = root;
            return;
        }
        else{
            currentNode = currentNode->Left;
            return insertItem(data);
        }
    }
    if(data>currentNode){
        if(currentNode->Right.is_empty()){
            currentNode->Right = new BTNode(data);
            currentNode = root;
            return;
        }
        else{
            currentNode = currentNode->Right;
            return insertItem(data);
        }
    }
    currentNode = root;
    return; 
}

void deleteItem(Type data){}
void is_empty(){
    if (this == NULL) return 1;
    else return 0;
}
void printInOrder(){                                
    if(!(currentNode->Left).is_empty()){
        currentNode = currentNode->Left;            
    }
}

private:
    BTNode<Type>* currentNode;
    BTNode<Type>* root;
};

这里是存储BinaryTree项目的BTNode类,并指向下一个Node:

template <class Type>
class BTNode{
public:

    friend class BinaryTree<Type>;

    BTNode(){}
    BTNode(Type data){
        Item = data;
    }
    ~BTNode(){}


private:
    Type Item;
    BTNode<Type> *Left, *Right;
};

二叉树类的 BTNode*root 指向第一个节点,currentNode 将在执行插入或合并节点等操作时指向“当前节点”。

但是当我编译时,BinaryTree 类中出现编译器错误 C2143, 这里:

BTNode<Type>* root;
BTNode<Type>* currentNode;

报错说&lt;前面没有toke ; 但我不知道出了什么问题

【问题讨论】:

    标签: binary-tree


    【解决方案1】:

    似乎BTNode 没有在BinaryTree 类中正确定义。编译器不理解 BTNode 是一种类型。确保正确包含和链接文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      相关资源
      最近更新 更多