二进制树插入()const fpermissive误差

Binary tree insert() const fpermissive error

本文关键字:const fpermissive 误差 插入 二进制      更新时间:2023-10-16

我必须为我的C 课程做自己的BinTree模板类。问题在于,在我的教授中,这全都是用const制成的,但是我的代码仅在我将const s淘汰时编译。在下面的代码中,我总是通过创建新节点在input()方法中收到错误。Netbeans抛出以下错误:

bintree.hpp:52:73:错误:从'const bintree :: node*'到'bintree :: node*'[-fpermissive]的转换无效: 键{ch},value {val},top {t},左{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{''}, 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;;
}
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);