二叉树插入()const fpermissive错误

问题描述 投票:0回答:1

我必须为我的C ++课程制作自己的BinTree模板类。问题是,在我的教授的例子中,它全部是用const制作的,但是我的代码只有在我把consts取出时编译。在下面的代码中,我总是通过创建新节点在input()方法中收到错误。 Netbeans引发以下错误:

BinTree.hpp:52:73:错误:从'const BinTree :: node *'无效转换为'BinTree :: node *'[-fpermissive]:key {ch},value {val},top {t},left {nullptr},右{nullptr},计数器{1} {}

我希望你能帮助我解决它。

    template <class T> class BinTree {

    struct node;  //forward deklaration

    public:

    BinTree(); 
    ~BinTree();

    int size() const {
        return sz;
    }
    node* find(const char& ) const;
    node* insert(const char&);
    node* getRoot(); 
    void in_Order();
    void level_Order(node*);
    void treeHigh();
    int count(node*);


    private:

    struct node {
        char key;
        T value;
        node* top; 
        node* left;
        node* right;
        volatile int counter;
        explicit node(const char&, const T& = T {}, const node* t = nullptr);

      };
      node* root;
      int sz; 
     };

    template<class T>
    BinTree<T>::BinTree() : root{'\0'}, sz{0} {}

    template<class T>
    BinTree<T>::node::node(const char& ch, const T& val, const node* t)
    : key{ch}, value {val}, top{t}, left{nullptr}, right{nullptr}, counter{1} {}      


    template<class T> typename BinTree<T>::node* 
    BinTree<T>::insert(const char& val) {
    node * n{root};
    node * nn{ new node{val}} ;


    if (root == nullptr) {
        n = nn;
    }

    while (n != nullptr) {

        if (nn->value == n->key) {
            n->counter++;
            delete nn;
            return n;
        }


        if (nn->value < n->key) {

            if (n->left == nullptr) {
                n->left = nn;
                nn->top = n;
                break;
            };
        } else n = n->left;
        continue;

        if (nn->value > n->key) {
            if (n->right == nullptr) {
                n->right = nn;
                nn->top = n;
                break;
            };

        } else n = n->right;
        continue;
    }
          ++sz;
          return nn;;
}
c++ const binary-tree binary-search-tree
1个回答
0
投票
struct node {
    char key;
    T value;
    node* top;  // has to be const node* top
    node* left;
    node* right;
    volatile int counter;
    explicit node(const char&, const T& = T {}, const node* t = nullptr);
© www.soinside.com 2019 - 2024. All rights reserved.