I'm attempting to write a simple B+tree implementation (very early stages). I've got a virtual class with a few functions. Needless to say, I'm very new to these strategies and am running into all sorts of problems.
I'm attempting to create a root node within the BTree class. The root node will be a BBranch, which should inherit from BNode? I'm getting errors
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
The code is this
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;
}
Thank you for any information you can give me.
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.
A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function.
While defining (providing an implementation) pure virtual methods is rarely useful, you must define a pure virtual destructor. This is because the destructor of a base class is always called when a derived object is destroyed.
A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax. For example: class Base {
The compiler seems to be pretty clear about what's wrong. You can't declare a BBranch
because there's still a pure virtual function in that class. You defined ins
, but del
is still undefined. Define that in BBranch
(and BLeaf
) and you should be fine.
You can't declare instances of abstract classes, which are classes that have pure virtual functions.
Furthermore, you have declared root
in the constructor. You meant for it to be a member variable, which means it needs to be declared beside the constructor, not inside.
class BTree {
public:
BTree() {
};
BBranch root;
void ins( int num );
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With