レイトレーサーを作ろうとしています。Shape というクラスがあり、これを Sphere (および三角形などの他の形状) のクラスに拡張しています。形には方法がある
virtual bool intersect(Ray) =0;
そこで、次のようにして Sphere クラスを作成します
class Sphere : public Shape{
public:
Sphere(){};
bool intersect(Ray){/*code*/};
};
Shape ポインターのリストを作成するために使用するメイン クラスがあります。Sphere ポインターを作成し、次の操作を行います。
Sphere* sph = &Sphere();
shapes.emplace_front(sph); //shapes is a list of Shape pointers
次に、別のクラスでレイをトレースする場合は、次のようにします。
for (std::list<Shape*>::iterator iter=shapes.begin(); iter != shapes.end(); ++iter) {
Shape* s = *iter;
bool hit = (*s).intersect(ray);
}
しかし、*s が Sphere タイプのオブジェクトを指しているはずなのに、仮想クラス Shape で intersect を呼び出すことができないというエラーが表示されます。継承で何が間違っていますか?