0

I have to make my own BinTree template class for my C++ course. The problem is that in the example from my prof it´s all made with const, but my code only compiles if I take the consts out. In the code below I always receive an error in the input() method by creating the new node. Netbeans throws the following error:

BinTree.hpp:52:73: error: invalid conversion from 'const BinTree::node*' to 'BinTree::node*' [-fpermissive] : key{ch}, value {val}, top{t}, left{nullptr}, right{nullptr}, counter{1} {}

I hope you can help me fix it.

    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;;
}
Ron
  • 14,674
  • 4
  • 34
  • 47
Pretzel
  • 1
  • 1
  • 1
    In the future please apply **C**apitalization rules, fix the indentation and consult the [help]. Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. This particular language can't be learned by guessing. – Ron Dec 16 '17 at 15:18
  • Do you understand the error message? Just a hint: Look at the type for the parameter `t` in your node constructor and the type of the member `top` you assign it to. Then ask yourself: Do you want to be able to modify the parent node from a child node? – Tobias Ribizel Dec 16 '17 at 15:27
  • @TobiasRitzel Thanks for your hint man, i was blind. Now it works. – Pretzel Dec 16 '17 at 16:32

1 Answers1

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);
Pretzel
  • 1
  • 1
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Dec 16 '17 at 19:52