类不是抽象的,但出现错误C2259:无法实例化抽象类 [英] Class isn't abstract but I get Error C2259: cannot instantiate abstract class
问题描述
我正在尝试在C ++中实现策略模式,但出现以下错误:
I'm trying to implement a Strategy pattern in C++, but I get the following error:
错误1错误C2259: LinearRootSolver:无法实例化抽象类
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
[...]
}
这是我的策略模式类:
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) {
[...]
}
};
我不明白为什么在尝试在intersect方法中实例化抽象类时出现错误
I can't see why I get an error for trying to instantiate an abstract class in the intersect method at the top?
推荐答案
void findRoot(Vector3* P, float a, float b, Ray& ray) = 0;
//^^
和
void findRoot(Vector3& P, float a, float b, Ray& ray)
//^^
参数类型不匹配,因此 findRoot
继承的基于表单的类仍然是纯虚函数(不可覆盖),这使 LinearRootSolver
类成为抽象类。当您这样做时:
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.
这篇关于类不是抽象的,但出现错误C2259:无法实例化抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!