分配抽象类类型 c++ xcode 的对象
Allocating an object of abstract class type c++ xcode
TreeInterface.h
#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,我不确定.我将不胜感激。
树.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);
};
' 节点.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;
};
当我尝试创建二进制搜索树的新实例时出现错误。 主.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();
}
正如 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 实际上仍然是一个抽象类。
比较您的函数:void setItem(ItemType item);
不会覆盖基类中的virtual void setItem()=0;
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 分配抽象类类型 c++ xcode 的对象
- xcode中的对象字节对齐
- 无法连接对象 xcode 4.2
- 如何在Xcode中引用图像和其他对象,以及将它们物理放置在何处
- Xcode中使用纯虚拟对象的链接器命令失败
- xcode中的c++:在main中调用自定义对象中的任何(!!!!)方法时出错
- 尝试处理嵌套对象/结构和动态数组时出现内存泄漏或内存错误。可能的 Xcode/malloc 问题
- Xcode,如何包含和声明我的对象