Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class isn't abstract but I get Error C2259: cannot instantiate abstract class

I'm trying to implement a Strategy pattern in C++, but I get the following error:

Error 1 error C2259: 'LinearRootSolver' : cannot instantiate abstract class

Here's my code (the line where the error is, is marked with a comment). Class that uses strategy pattern (context):

bool Isosurface::intersect(HitInfo& result, const Ray& ray, float tMin, float tMax) {
    INumericalRootSolver *rootSolver = new LinearRootSolver(); // error here
    [...]
}

And here's my strategy pattern classes:

class INumericalRootSolver {
public:
    virtual void findRoot(Vector3* P, float a, float b, Ray& ray) = 0;
};

class LinearRootSolver : public INumericalRootSolver {
public:
    void findRoot(Vector3& P, float a, float b, Ray& ray) {
        [...]
    }
};

I can't see why I get an error for trying to instantiate an abstract class in the intersect method at the top?

like image 403
Marc Ilsø Poulsen Avatar asked Jan 13 '23 08:01

Marc Ilsø Poulsen


2 Answers

 void findRoot(Vector3* P, float a, float b, Ray& ray) = 0;
                    //^^

and

void findRoot(Vector3& P, float a, float b, Ray& ray) 
              //^^

parameter type mismatch, so findRoot inherited form based class is still a pure virtual function (not overriding), which make the LinearRootSolver class an abstract class. When you do:

  INumericalRootSolver *rootSolver = new LinearRootSolver();

it tries to create an object of abstract class, you got the compiler error.

like image 112
taocp Avatar answered Jan 28 '23 17:01

taocp


Your definition for LinearRootSolver::findRoot has the wrong signature. In particular, the first argument should be a pointer according to the declaration in INumericalRootSolver:

void findRoot(Vector3* P, float a, float b, Ray& ray) {
//                   ^ Here
    [...]
}

In C++11, you can avoid this mistake by using the override keyword:

void findRoot(Vector3& P, float a, float b, Ray& ray) override {
    [...]
}

This wouldn't compile because the function doesn't override a function from the base class.

like image 38
Joseph Mansfield Avatar answered Jan 28 '23 19:01

Joseph Mansfield