I am working with traversals for the first time and am running into some errors with my them I am just going to post my preorder code because I am gettin the same error for post and in order.

The Error:
BSTree.h: In member function âvoid BSTree<T>::printPreorder() const [with T = int]â:
assign6.cpp:30: instantiated from here
BSTree.h:198: error: passing âconst BSTree<int>â as âthisâ argument of âvoid BSTree<T>::pre_trav(TNode<T>*) [with T = int]â discards qualifiers
BSTree.h: In member function âvoid BSTree<T>::printInorder() const [with T = int]â:

template <class T>
        void BSTree<T>::printPreorder() const
        {
        if (root != NULL)
                pre_trav(root);
        }

        template <class T>
        void BSTree<T>::pre_trav(TNode<T>* ptr)
        {
                cout << ptr->data << " ";
                if(ptr->left != NULL)
                        pre_trav(ptr->left);
                if(ptr->right != NULL)
                        pre_trav(ptr->right);
        }

I was able to correct the errors with that but I am having a difficult time with my assignment operator.

ERROR:
BSTree.h: In member function âconst BSTree<T>& BSTree<T>::operator=(const BSTree<T>&) [with T = int]â:
assign6.cpp:71: instantiated from here
BSTree.h:88: error: no matching function for call to âBSTree<int>::destroyTree(BSTree<int>* const)â
BSTree.h:172: note: candidates are: void BSTree<T>::destroyTree(TNode<T>*) [with T = int]
assign6.cpp:71: instantiated from here
BSTree.h:89: error: no matching function for call to âBSTree<int>::copyTree(const BSTree<int>*)â
BSTree.h:183: note: candidates are: TNode<T>* BSTree<T>::copyTree(TNode<T>*) [with T = int]

template <class T>
        const BSTree<T>& BSTree<T>::operator=(const BSTree<T>& rightOp)
        {
        if (this != &rightOp)
                {
                destroyTree(this);
                copyTree(&rightOp);
                }
        }

#

Assignment operator

Standard approach applies. Not much new here that you haven't done before. Deleting the storage for the left operand can be done by calling destroyTree; copying the storage of the right operand can be done by calling copyTree().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.