【发布时间】:2010-11-13 10:54:07
【问题描述】:
我正在尝试编写一个简单的 B+tree 实现(非常早期的阶段)。我有一个带有一些功能的虚拟类。不用说,我对这些策略非常陌生,并且遇到了各种各样的问题。
我正在尝试在 BTree 类中创建一个根节点。根节点将是一个 Bbranch,应该从 BNode 继承?我遇到了错误
btree.cpp: In constructor âBTree::BTree()â:
btree.cpp:25: error: cannot declare variable ârootâ to be of abstract type âBBranchâ
btree.cpp:12: note: because the following virtual functions are pure within âBBranchâ:
btree.cpp:9: note: virtual void BNode::del(int)
btree.cpp: In member function âvoid BTree::ins(int)â:
btree.cpp:44: error: ârootâ was not declared in this scope
代码是这样的
using namespace std;
class BNode {
public:
int key [10];
int pointer [11];
virtual void ins( int num ) =0;
virtual void del( int num ) =0;
};
class BBranch: public BNode {
public:
void ins( int num );
};
class BLeaf: public BNode {
public:
void ins( int num );
};
class BTree {
public:
BTree() {
BBranch root;
};
void ins( int num );
};
// Insert into branch node
void BBranch::ins( int num ){
// stuff for inserting specifically into branches
};
// Insert for node
void BTree::ins( int num ){
root.ins( num );
};
int main(void){
return 0;
}
感谢您提供的任何信息。
【问题讨论】:
-
我要感谢大家的回复...我承认,我不是一个出色的 C++ 程序员,我正在尝试将其中一些新想法(虚拟类/函数)应用到这个项目中。我也很难确定错误代码的正面或反面,但你们都做得很好,帮助我看到了我的错误。谢谢!
标签: c++ inheritance polymorphism