2017-07-29 53 views
-1

TreeInterface.h分配抽象类C类对象++的Xcode

#ifndef TreeInterface_h 
#define TreeInterface_h 
#include"PreconditionException.h" 
#include"NotFoundException.h" 
//#include"Tree.hpp" 
template<class ItemType> 
class TreeInterface //: public binarySearchTree<ItemType> 
{ 
    virtual void clear()=0; 
    virtual bool isEmpty()const=0; 
    virtual int getHeight()=0; 
    virtual ItemType getRootData() const throw(precondViolated)=0; 
    virtual bool add(const ItemType& item)=0; 
    virtual void setItem()=0; 
    virtual int getNumberOfNodes()const=0; 
    //virtual ItemType getEntry(const ItemType& anEntry) const throw(NotFoundException)=0; 
    // int getNumberOfNodes()const; 
    //virtual void setRootData(const ItemType& item)=0; 
    //virtual void inorder()=0; 
}; 
#endif /* TreeInterface_h */ 

我尝试创建一个二叉树,但我有一个抽象类的问题。当我尝试创建类binarySearchTree的新实例时,它给了我一个错误:Allocating an object of abstract class type "binarySearchTree"。我检查了我所有的功能,我不知道该怎么做。我认为问题是包含不同的文件,如Node.cpp,我不确定它,我会很感激一些帮助。

tree.h中

#ifndef Tree_h 
#define Tree_h 
#include"TreeInterface.h" 
#include"Node.h" 
//#include"tree.cpp" // should be correct 
#include <stdio.h> 
#include <iostream> 
#include<cstdlib> 
#include"PreconditionException.h" 
#include "NotFoundException.h" 
using namespace std; 
template<class ItemType> 
class binarySearchTree: public TreeInterface<ItemType> 
{ 
private: 
    node<ItemType>* rootPtr; 
protected: 
int getHeightHelp(node<ItemType>* subTreePtr)const; 

void destroyTree(node<ItemType>* subTreePtr); 

node<ItemType>* balancedAdd(node<ItemType>* subTreePtr,node<ItemType>* newNodePtr); 

node<ItemType>* copyTree(const node<ItemType>* treePtr) const; 
public: 
binarySearchTree(); 

binarySearchTree(const ItemType& rootItem); 

binarySearchTree(const ItemType& rootItem,binarySearchTree<ItemType>* leftPart,binarySearchTree<ItemType>* rightPart); 

binarySearchTree(const binarySearchTree<ItemType>& treePtr); 

void clear(); 
bool isEmpty()const; 
int getHeight(); 
bool add(const ItemType& item); 
ItemType getRootData() const throw(precondViolated); 
int getNumberOfNodes(node<ItemType>* subtree)const; 
void setItem(ItemType item); 
}; 

` Node.h

#ifndef Node_h 
#define Node_h 

#include <stdio.h> 
#include<iostream> 
using namespace std; 
template<class ItemType> 
class node 
{ 
private: 
ItemType data; 
node<ItemType>* left; 
node<ItemType>* right; 
public: 
node(); 
node(const ItemType &newdata); 
node(const ItemType& item,node<ItemType>* leftPtr,node<ItemType>*  rightPtr); 
ItemType getNodeItem(); 
ItemType* getLeftPtr(); 
ItemType* getRightPtr(); 
void setLeft(node<ItemType>* newleft); 
void setRight(node<ItemType>* newright); 
void setNodeItem(ItemType& item); 
bool isLeaf() const; 
}; 

,当我尝试创建binarySearchTree的新实例的错误出现。 main.cpp中

#include <iostream> 
#include<cstdlib> 
#include<string> 
#include"Tree.h" 
using namespace std; 
int main() 
{ 
int num=11; 
binarySearchTree<int>* node=new binarySearchTree<int>(); //the error is here. Allocating an object of abstract class type "binarySearchTree" 
node->add(9); 
node->isEmpty(); 
} 
+1

?这似乎不太可能。请查看[mcve]以了解在堆栈溢出问题中发布代码的适当方式。 – xaxxon

+0

[“无法分配抽象类型的对象”错误]的可能的重复(https://stackoverflow.com/questions/7352706/cannot-allocate-an-object-of-abstract-type-error) – xaxxon

回答

0

正如xaxxon指出与链接,如果有一个抽象类(其中,虚拟函数= 0),则所有的在基类的功能的需要被过骑在阶实例化派生类的一个对象。你的编译器错误告诉你,你还没有覆盖所有的功能。

在你的情况下,你的问题稍微微妙一些。考虑以下几点:

class Abstract 
{ 
public: 
    virtual bool MyFunction(int x) = 0; 
}; 

class Concrete : public Abstract 
{ 
public: 
    bool MyFunction()  // This does not override Abstract::MyFunction because it is "overloaded", parameters are different 
    { 
     return true; 
    } 
}; 

int main() 
{ 
    Concrete concrete; 
    return 0; 
} 

虽然可能会出现,我们是压倒一切的MyFunction,这种情况并非如此,因为这些参数是不同的(基类有INT X)。所以它的功能不一样,Concrete实际上仍然是一个抽象类。

比较你的功能:需要此代码的每一行重现您的问题在骑virtual void setItem()=0;void setItem(ItemType item);不会在基类

+0

您不应该回答那个问题中的重复问题。如果您认为重复信息不足,您应该在那里添加答案以保持信息集中 - 但大多数情况下,现有答案已足够,而其他答案只会使得获得最佳答案所需的时间更长。 – xaxxon