2016-04-25 29 views
1

私は宿題用にこのプログラムを作成しようとしていますが、createList関数を追加するまでC2259エラーが発生しませんでした。 この問題がどこから発生しているのかわかりません。私はC++でかなり新しいですので、これは簡単な修正かもしれませんが、私は完全に失われています。ここでエラーが発生しました。C2259:抽象クラスをインスタンス化できません。

は私の主な機能である:ここでは

#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 with -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; 
} 

は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 to determine if searchItem is 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 an 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 to be inserted is already in the tree"; 
       cout << "Duplicates are 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 from an 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 to be deleted is not in the 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 to be deleted is not in the 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: The node to be deleted is 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 

また、私は私のcreateList機能は非常に間違っている112パーセント確信しているとのツリーを作成しません乱数なので、私はそれにも少し役立つことができます。


更新:binaryTree.h定義

#ifndef H_binaryTree 
#define H_binaryTree 

#include<iostream> 

using namespace std; 

//Define the 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 the 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 the binary tree is not empty, 
      //destroy the binary tree 
      destroy(root); 

     if (otherTree.root == NULL) //otherTree is 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 is 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 
+0

binaryTreeType <>の定義は何ですか? – Tim

+0

私はbinaryTree.hファイルを元の投稿に追加しました。更新 –

答えて

2
virtual void createList(const elemType& newList) = 0 ; 
          ///     ~~~~ shouldn't be used in derived class 

純粋仮想関数だけで、私はあなたがcreateListメソッドをオーバーライドする必要がbinaryTreeType

0

であると仮定し、あなたの抽象クラスに存在すべきであり、それは仮想だからです。

+0

を参照してください。しかし、抽象クラス内にあなたの仮想メソッドをまだ実装していません。 –

関連する問題