c++ - Helpt with error C2259: cannot instantiate abstract class -
i trying write program homework, , wasn't throwing c2259 error until added createlist function. can't figure out problem originating from. new @ c++ might easy fix, lost.
here main function:
#include <iostream> #include "binarysearchtree.h" #include "orderedlinkedlist.h" using namespace std; int main() { bsearchtreetype<int> treeroot; //error c2259: 'bsearchtreetype<int>' //cannot instantiate abstract class orderedlinkedlist<int> newlist; int num; cout << "enter numbers ending -999" << endl; cin >> num; while (num != -999) { treeroot.insert(num); cin >> num; } cout << endl << "tree nodes in inorder: "; treeroot.inordertraversal(); cout << endl; cout << "tree height: " << treeroot.treeheight() << endl; treeroot.createlist(newlist); cout << "newlist: "; newlist.print(); cout << endl; system("pause"); return 0; }
here binarysearchtree.h:
//header file binary search tree #ifndef h_binarysearchtree #define h_binarysearchtree #include <iostream> #include"binarytree.h" using namespace std; template<class elemtype> class bsearchtreetype : public binarytreetype < elemtype > { public: //function determine if searchitem in search tree bool search(const elemtype& searchitem) const; void insert(const elemtype& insertitem); void deletenode(const elemtype& deleteitem); //update: void createlist(const elemtype& createitem); virtual void createlist(const elemtype& newlist) = 0; private: void deletefromtree(nodetype<elemtype> *&p); }; template<class elemtype> void bsearchtreetype<elemtype>::createlist(const elemtype& createitem) { nodetype<elemtype> *current; nodetype<elemtype> *trailcurrent; nodetype<elemtype> *newnode; newnode = new nodetype < elemtype > ; newnode->info = createitem; newnode->llink = nullptr; newnode->rlink = nullptr; if (root == nullptr) { root = newnode; } } template<class elemtype> bool bsearchtreetype<elemtype>::search(const elemtype& searchitem)const { nodetype<elemtype> *current; bool found = false; if (root == null) { cout << "cannot search empty tree." << endl; } else { current = root; while (current != null && !found) { if (current->info == searchitem) { found = true; } else if (current->info > searchitem) { current = current->llink; } else { current = current->rlink; } } } return found; } template<class elemtype> void bsearchtreetype<elemtype>::insert(const elemtype& insertitem) { nodetype<elemtype> *current; nodetype<elemtype> *trailcurrent; nodetype<elemtype> *newnode; newnode = new nodetype < elemtype > ; newnode->info = insertitem; newnode->llink = null; newnode->rlink = null; if (root == null) { root = newnode; } else { current = root; while (current != null) { trailcurrent = current; if (current->info == insertitem) { cout << "the item inserted in tree"; cout << "duplicates not allowed." << endl; return; } else if (current->info > insertitem) { current = current->llink; } else { current = current->rlink; } } if (trailcurrent->info > insertitem) { trailcurrent->llink = newnode; } else { trailcurrent->rlink = newnode; } } } template<class elemtype> void bsearchtreetype<elemtype>::deletenode(const elemtype& deleteitem) { nodetype<elemtype> *current; nodetype<elemtype> *trailcurrent; bool found = false; if (root == null) { cout << "cannot delete empty tree." << endl; } else { current = root; trailcurrent = root; while (current != null && !found) { if (current->info == deleteitem) { found = true; } else { trailcurrent = current; if (current->info > deleteitem) { current = current->llink; } else { current = current->rlink; } } } if (current == null) { cout << "the item deleted not in tree." << endl; } else if (found) { if (current == root) { deletefromtree(root); } else if (trailcurrent->info > deleteitem) { deletefromtree(trailcurrent->llink); } else { deletefromtree(trailcurrent->rlink); } } else { cout << "the item deleted not in tree." << endl; } } } template<class elemtype> void bsearchtreetype<elemtype>::deletefromtree(nodetype<elemtype>* &p) { nodetype<elemtype> *current; nodetype<elemtype> *trailcurrent; nodetype<elemtype> *temp; if (p == null) { cout << "error: node deleted null." << endl; } else if (p->llink == null && p->rlink == null) { temp = p; p = null; delete temp; } else if (p->llink == null) { temp = p; p = temp->rlink; delete temp; } else if (p->rlink == null) { temp = p; p = temp->llink; delete temp; } else { current = p->llink; trailcurrent = null; while (current->rlink != null) { trailcurrent = current; current = current->rlink; } p->info = current->info; if (trailcurrent == null) { p->llink = current->llink; } else { trailcurrent->rlink = current->llink; } delete current; } } #endif
also, 112% sure createlist function wrong , not create tree of random numbers, use little on well.
update: binarytree.h definition
#ifndef h_binarytree #define h_binarytree #include<iostream> using namespace std; //define node template<class elemtype> struct nodetype { elemtype info; nodetype<elemtype> *llink; nodetype<elemtype> *rlink; }; template<class elemtype> class binarytreetype { public: const binarytreetype<elemtype>& operator=(const binarytreetype<elemtype>&); bool isempty() const; void inordertraversal() const; void preordertraversal() const; void postordertraversal() const; int treeheight() const; int treenodecount() const; int treeleavescount() const; void destroytree(); virtual bool search(const elemtype& searchitem) const = 0; virtual void insert(const elemtype& insertitem) = 0; virtual void deletenode(const elemtype& deleteitem) = 0; virtual void createlist(const elemtype& createitem) = 0; binarytreetype(const binarytreetype<elemtype>& othertree); binarytreetype(); ~binarytreetype(); protected: nodetype<elemtype> *root; private: void copytree(nodetype<elemtype>*& copiedtreeroot, nodetype<elemtype>* othertreeroot); void destroy(nodetype<elemtype>* &p); void inorder(nodetype<elemtype> *p)const; void preorder(nodetype<elemtype> *p)const; void postorder(nodetype<elemtype> *p) const; int height(nodetype<elemtype> *p)const; int max(int x, int y) const; int nodecount(nodetype<elemtype> *p)const; int leavescount(nodetype<elemtype> *p)const; }; template <class elemtype> binarytreetype<elemtype>::binarytreetype() { root = null; } template <class elemtype> bool binarytreetype<elemtype>::isempty() const { return (root == null); } template <class elemtype> void binarytreetype<elemtype>::inordertraversal() const { inorder(root); } template <class elemtype> void binarytreetype<elemtype>::preordertraversal() const { preorder(root); } template <class elemtype> void binarytreetype<elemtype>::postordertraversal() const { postorder(root); } template <class elemtype> int binarytreetype<elemtype>::treeheight() const { return height(root); } template <class elemtype> int binarytreetype<elemtype>::treenodecount() const { return nodecount(root); } template <class elemtype> int binarytreetype<elemtype>::treeleavescount() const { return leavescount(root); } template <class elemtype> void binarytreetype<elemtype>::copytree(nodetype<elemtype>* &copiedtreeroot, nodetype<elemtype>* othertreeroot) { if (othertreeroot == null) copiedtreeroot = null; else { copiedtreeroot = new nodetype<elemtype>; copiedtreeroot->info = othertreeroot->info; copytree(copiedtreeroot->llink, othertreeroot->llink); copytree(copiedtreeroot->rlink, othertreeroot->rlink); } } template <class elemtype> void binarytreetype<elemtype>::inorder(nodetype<elemtype> *p) const { if (p != null) { inorder(p->llink); cout << p->info << " "; inorder(p->rlink); } } template <class elemtype> void binarytreetype<elemtype>::preorder(nodetype<elemtype> *p) const { if (p != null) { cout << p->info << " "; preorder(p->llink); preorder(p->rlink); } } template <class elemtype> void binarytreetype<elemtype>::postorder(nodetype<elemtype> *p) const { if (p != null) { postorder(p->llink); postorder(p->rlink); cout << p->info << " "; } } //overload assignment operator template <class elemtype> const binarytreetype<elemtype>& binarytreetype<elemtype>::operator=(const binarytreetype<elemtype>& othertree) { if (this != &othertree) //avoid self-copy { if (root != null) //if binary tree not empty, //destroy binary tree destroy(root); if (othertree.root == null) //othertree empty root = null; else copytree(root, othertree.root); }//end else return *this; } template <class elemtype> void binarytreetype<elemtype>::destroy(nodetype<elemtype>* &p) { if (p != null) { destroy(p->llink); destroy(p->rlink); delete p; p = null; } } template <class elemtype> void binarytreetype<elemtype>::destroytree() { destroy(root); } //copy constructor template <class elemtype> binarytreetype<elemtype>::binarytreetype (const binarytreetype<elemtype>& othertree) { if (othertree.root == null) //othertree empty root = null; else copytree(root, othertree.root); } //destructor template <class elemtype> binarytreetype<elemtype>::~binarytreetype() { destroy(root); } template<class elemtype> int binarytreetype<elemtype>::height (nodetype<elemtype> *p) const { if (p == null) return 0; else return 1 + max(height(p->llink), height(p->rlink)); } template <class elemtype> int binarytreetype<elemtype>::max(int x, int y) const { if (x >= y) return x; else return y; } template <class elemtype> int binarytreetype<elemtype>::nodecount(nodetype<elemtype> *p) const { return nodecount(root); } template <class elemtype> int binarytreetype<elemtype>::leavescount(nodetype<elemtype> *p) const { return leavescount(root); } #endif
virtual void createlist(const elemtype& newlist) = 0 ; /// ~~~~ shouldn't used in derived class
the pure virtual function should present in abstract class assume binarytreetype
Comments
Post a Comment