Mar*_*sen 3 c++ compiler-errors visual-c++
我正在尝试用C++实现策略模式,但是我收到以下错误:
错误1错误C2259:'LinearRootSolver':无法实例化抽象类
这是我的代码(错误所在的行,标有注释).使用策略模式(上下文)的类:
bool Isosurface::intersect(HitInfo& result, const Ray& ray, float tMin, float tMax) {
INumericalRootSolver *rootSolver = new LinearRootSolver(); // error here
[...]
}
Run Code Online (Sandbox Code Playgroud)
这是我的策略模式类:
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) {
[...]
}
};
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我试图在顶部的交叉方法中实例化一个抽象类时出错?
void findRoot(Vector3* P, float a, float b, Ray& ray) = 0;
//^^
Run Code Online (Sandbox Code Playgroud)
和
void findRoot(Vector3& P, float a, float b, Ray& ray)
//^^
Run Code Online (Sandbox Code Playgroud)
参数类型不匹配,所以findRoot继承的基于表的类仍然是一个纯虚函数(不是重写),这使得LinearRootSolver该类成为一个抽象类.当你这样做时:
INumericalRootSolver *rootSolver = new LinearRootSolver();
Run Code Online (Sandbox Code Playgroud)
它试图创建一个抽象类的对象,你得到了编译错误.
| 归档时间: |
|
| 查看次数: |
12010 次 |
| 最近记录: |