David Querol
David Querol

Reputation: 31

Error - Variable type "X" is an abstract class - c++

I was trying to find the solution to my problem, but I don´t progress in any way, so I need you to help with this.

my abstract class is:

class Expression
{
public:
    Expression();
    virtual ~Expression();
    virtual double evaluate() const = 0; // calculates and returns the value of expression
    virtual void print() const = 0; // prints the mathematical expression as string
};

my Subclass:

class Const : public Expression
{
public:
    Const(double valIn);

    double evaluate();

    void print() const;

private:
    double val;
};

and the line I am having problems with is:

Const c(4);

from the function:

void testConst()
{
    Const c(4);
    c.print();
    std::cout << " = " << c.evaluate() << std::endl;
}

If I should post some more information, I will gladly edit the text. Thank you in advance

Upvotes: 1

Views: 9152

Answers (1)

EGOrecords
EGOrecords

Reputation: 1969

Const::evaluate() has a different signature than Expression::evaluate() const. Change your second expression to be const, then it works.

With C++11 there is the new override keyword, where the compiler gives you a better error message, if you have no matching signature in the parent class. http://en.cppreference.com/w/cpp/language/override

Upvotes: 3

Related Questions