Jacob Anderson
Jacob Anderson

Reputation: 1615

I get the error: cannot declare variable ‘a’ to be of abstract type ‘A’

Here is my code:

#include <iostream>

using namespace std;

class Base
{
    public:
        virtual void Sub1() = 0;
        virtual void Sub2();
        virtual void Sub3();
        void Sub4();
};

class A : public Base
{
    public:
        void Sub2();
        void Sub4();
};

class B : public A
{
    public:
        virtual void Sub1();
        void Sub2();
};

class C : public Base
{
    public:
        virtual void Sub1();
        virtual void Sub4();
};

void Base::Sub2()
{
    cout << "Hello from Base::Sub2()" << endl;
}

void Base::Sub3()
{
    cout << "Hello from Base::Sub3()" << endl;
    Sub1(); // DONT MISS THIS CALL IN YOUR ANSWER
}

void Base::Sub4()
{
    cout << "Hello from Base::Sub4()" << endl;
}

void A::Sub2()
{
    cout << "Hello from A:Sub2()" << endl;
}
void A::Sub4()
{
    cout << "Hello from A:Sub4()" << endl;
}

void B::Sub1()
{
    cout << "Hello from B:Sub1()" << endl;
}
void B::Sub2()
{
    cout << "Hello from B:Sub2()" << endl;
}

void C::Sub1()
{
    cout << "Hello from C:Sub1()" << endl;
}
void C::Sub4()
{
    cout << "Hello from C:Sub4()" << endl; //error used to say from Sub2
}

void Sub(Base& x)
{
    x.Sub1();
    x.Sub2();
    x.Sub3();
}
void AnotherSub(A& a)
{
    a.Sub1();
    a.Sub2();
    a.Sub4();
}

int main()
{
    A a; // wont compile
    B b;
    C c;
    Sub(a);
    Sub(b);
    Sub(c);
    AnotherSub(b);
}

I'm having trouble with the A a; and obviously the end Sub(a); because a cannot be used, but at the very end, where it says, "error: cannot declare variable ‘a’ to be of abstract type ‘A’" Any help would be appreciated, Thanks.

--- Also if it helps, the output without the A a; works and looks like:

    Hello from B:Sub1()
    Hello from B:Sub2()
    Hello from Base::Sub3()
    Hello from B:Sub1()
    Hello from C:Sub1()
    Hello from Base::Sub2()
    Hello from Base::Sub3()
    Hello from C:Sub1()
    Hello from B:Sub1()
    Hello from B:Sub2()
    Hello from A:Sub4()

Upvotes: 0

Views: 5283

Answers (4)

Ali Kazmi
Ali Kazmi

Reputation: 1458

What I want to add is Since Base is abstract and class A doesn't implement one of its pure virtual function so A is also abstract. As abstract class cant be instantiated (only pointers and references are allowed) we use pointers of abstract class (say pointer to A) to instantiate object of classes derived from abstract class (A or base in this case). So you need to provide implementation of pure virtual function and have to use pointer to A like

A *a;
a = new B(); // it would compile now

Hope you got my point.

Upvotes: 0

user3511557
user3511557

Reputation:

You are extending Base class from A but you didn't implement the pure virtual method in A. So, you need to make A as abstract class. It can't be a concrete class.

To ignore making A as abstract and to use it as a concrete class, just override the pure virtual method too and define it with no statements in it. Now you can create an instance for A.

Upvotes: 0

CiaPan
CiaPan

Reputation: 9570

Your compiler is right: you cannot create a variable of abstract typ. Your class Base is abstract because it declares a pure virtual method, that is a function without any implementation:

    virtual void Sub1() = 0;

Your class A derived from Base is abstract, too, because it does not provide an overriding implementation for the method.

You must provide an implementation to all declared virtual methods to make a type instantiable. As you override a pure virtual Sub1() in class B, you can create variables of class B (and its descendant classes), but class A cannot be used itself as an actual type for a variable.

Upvotes: 0

Aracthor
Aracthor

Reputation: 5907

This line into your class Base:

virtual void Sub1() = 0;

Make the class Base or any class extending it an abstract class if you don't implement the function Sub1, which is the case of your class A.

You cannot create instances of abstract classes, as it has methods with no implementation, which means impossible to call. This kind of classes can only be used through inheritance.

So you need to implement it directly on Base or A if you want to create A instances.

Upvotes: 5

Related Questions