views:

2143

answers:

5

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.

+6  A: 

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 );
};
Rob Kennedy
+1  A: 

In the BTree constructor, you are trying to create an instance of BBranch. But BBranch does not have an implementation of del(), hence it is an abstract class and cannot be instantiated. For a class to be instantiated, it must be concrete (i.e. all its member functions must have an implementation).

flodin
+1  A: 

The first error has to do with the fact that BBranch provides no override for BNode::del. Since del is a pure virtual function (it has no default implementation), any class that inherits BNode must either provide an implementation of del, or that class will be abstract, i.e. it cannot be instantiated.

The second error is because you have no BTree::root member variable. You declare root within the constructor of BTree, and then it's destroyed when the constructor finishes, so when you try to access root within BTree::ins, root is out of scope (and the object itself is likely destroyed).

Sean Nyman
+1  A: 

If you create an abstract base class like you have done with BNode and you want to create a concrete derived class, you must implement all pure virtual functions. Both BBranch and BLeaf miss an implementation of 'del' so they remain abstract.

Timo Geusch
+1  A: 

The error message is giving you all the info you need. Read it.

You are trying to create a variable of type BBranch, but your class BBranch is an abstract class, just like BNode, from which it is inheriting from.

I smell this is a homework question, so I'll encourage you to go back to your textbook and read about abstract classes, how are they used and more importantly, what is needed to end the "abstractness".

Jorge Gajon