C ++ error: cannot declare field of abstract type

In this situation:

class Base {  
    virtual void methodA() = 0;  
    virtual void methodB() = 0;  
};

class Base_A : public Base {  
    void methodA(); 
    void methodB();  
};

class Base_B : public Base {  
    void methodA();  
    void methodB();  
};

class MyClass {
    private:  
        Base * b;   
};  

      

When compiling, it gives an error message:

error: cannot declare field MyClass::b to be of abstract type because the following virtual functions are pure within Base:
Base::methodA()
Base::methodB()


How to solve this?

UPDATE Now it compiles. I don't know what I changed.

+2


source to share


2 answers


The code looks correct and perfect.

Your base class is abstract, so you cannot create it, but you can definitely declare a pointer to it.



So you could write Base b instead of Base * b, please check it.

+4


source


Your code snippets compile fine on my computer. Are you sure you are using Base* b

, i.e. pointer type, not Base b

?



+2


source







All Articles