BecomeBetter
BecomeBetter

Reputation: 433

How to instantiate a class derived from a abstract class

template<class Elem>
class BinNode//Binary tree node abstract class.
{
public:
    virtual Elem& val() = 0;//return the node's element;
    virtual void setVal(const Elem& ) = 0;//set the node's element;
    virtual BinNode* left() const = 0;//return the node's left child;
    virtual BinNode* right() const = 0;//return the node's right child;
    virtual void setLeft(BinNode*) = 0;//set the node's left child's element;
    virtual void setRight(BinNode*) = 0;//set the node's right child's element;
};

template<class Elem>
class BinNodePtr:public BinNode<Elem>
{
public:
    Elem ele;//the content of the node
    BinNodePtr<Elem>* lc;//the node's left child
    BinNodePtr<Elem>* rc;//the node's right child
public:
    static int now_on;//the node's identifier
    BinNodePtr(){lc = rc = NULL;ele = 0;};//the constructor
    ~BinNodePtr(){};//the destructor
    void setVal(const Elem& e){this->ele = e;};
    Elem& val(){return ele;}//return the element of the node
    inline BinNode<Elem>* left()const//return the node's left child
    {
        return lc;
    }
    inline BinNode<Elem>* right()const//return the node's right child
    {
        return rc;
    }
    void setLeft(const Elem& e){lc->ele = e;}//to set the content of the node's leftchild
    void setRight(const Elem& e){rc->ele = e;}//to set the content of the node's rightchild
};

int main()
{
    BinNodePtr<int> root;//initiate a variable//the error is here.
    BinNodePtr<int> subroot = &root;//initiate a pointer to the variable
}

When I build it , the compiler told me that "cannot instantiate abstract class". And the error is at BinNodePtr root; I'm not instantiating an abstract class but a class derived from an abstract class.How can I solve it ?

Upvotes: 0

Views: 177

Answers (3)

BlackMamba
BlackMamba

Reputation: 10254

Because you don't implement these pure virtual functions

virtual void setLeft(BinNode*) = 0;//set the node's left child's element;
virtual void setRight(BinNode*) = 0;//set the node's right child's element;

So the class BinNodePtr is an abstract class. And there is an import rule: you can instantiate an abstract class, that say you can't create an class object using abstract class.

you can read this article:

Upvotes: 0

Hitesh Vaghani
Hitesh Vaghani

Reputation: 1362

In abstract class.

virtual void setLeft(**BinNode***) = 0;//set the node's left child's element;
virtual void setRight(**BinNode***) = 0;//set the node's right child's element;

and in derived class

void setLeft(const **Elem**& e){lc->ele = e;}
void setRight(const **Elem**& e){rc->ele = e;}

Function signature doesn't match..

Upvotes: 2

feihu
feihu

Reputation: 1953

The following two methods are not defined:

virtual void setLeft(BinNode*) = 0;//set the node's left child's element;
virtual void setRight(BinNode*) = 0;//set the node's right child's element;

Because the defined methods in BinNodePtr have different parameters:

void setLeft(const Elem& e){lc->ele = e;}
void setRight(const Elem& e){rc->ele = e;}

Upvotes: 1

Related Questions