我正在尝试为家庭作业编写这个程序,并且在添加createList函数之前它没有抛出C2259错误。 我无法弄清楚这个问题的起源。我在c ++方面相当新,所以这可能很容易解决,但我完全迷失了。
这是我的主要功能:
#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
此外,我112%确定我的createList函数非常错误,并且不会创建随机数树,所以我也可以使用一点帮助。
更新: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 :(得分:2)
virtual void createList(const elemType& newList) = 0 ;
/// ~~~~ shouldn't be used in derived class
纯虚函数应该只存在于我假设为binaryTreeType
答案 1 :(得分:0)
您必须覆盖createList方法,因为它是虚拟的。