C++ 从两个具有相同功能原型的类继承而来的类彼此冲突

C++ 从两个具有相同功能原型的类继承而来的类彼此冲突,c++,inheritance,C++,Inheritance,我正在执行光线跟踪任务,以下是有问题的来源: class Geometry { public: virtual RayTask *intersectionTest(const Ray &ray) = 0; }; class Sphere : public Geometry { public: RayTask *intersectionTest(const Ray &ray); }; class BoundingVolume {

我正在执行光线跟踪任务,以下是有问题的来源:

class Geometry
{
    public:
        virtual RayTask *intersectionTest(const Ray &ray) = 0;
};

class Sphere : public Geometry
{
    public:
        RayTask *intersectionTest(const Ray &ray);
};

class BoundingVolume
{
    public:
        virtual bool intersectionTest(const Ray &ray) = 0;
};

class BoundingSphere : public Sphere, BoundingVolume
{
    public:
        bool intersectionTest(const Ray &ray) // I want this to be inherited from BoundingVolume
        {
            return Sphere::intersectionTest(ray) != NULL; // use method in Sphere
        }
};
上面的源代码无法编译,错误信息:

error: conflicting return type specified for ‘virtual bool BoundingSphere::intersectionTest(const Ray&)’
error:   overriding ‘virtual RayTask Sphere::intersectionTest(const Ray&)
我想使用Sphere中的方法实现BoundingSphere::intersectionTest,所以我需要继承BoundingVolume和Sphere。但由于继承的函数具有相同的参数列表和不同的返回类型,所以事情变得一团糟

我不想重复具有相同功能的代码。。。
有谁能给我一个解决方案吗?…

编译器正试图用不同的返回类型重写两个虚拟方法,这是不允许的:如果编译器不知道返回类型将是什么,它如何知道为函数调用分配多少内存?两个方法不能有相同的名称;试着把一个换成更合适的意思

如果你觉得这些名字最能代表他们所提供的行动的含义(我不确定),我也建议你仔细考虑你的等级结构。球形的
边界体积
真的是
球体
?也许不是:它是根据
Sphere
实现的(私有继承,不能解决您的问题),或者它有一个
Sphere
(在这个简单的例子中,组合可以解决您的问题)。不过,后一种情况可能会给移动复杂类带来问题,因为您希望
BoundingSphere
具有
Sphere
的所有方法。或者,您是否需要区分
边界体积
和普通
几何体
s


该问题的另一个解决方案是对其中一个层次结构使用非成员函数,并使用Koenig lookup(参数的类型)调用适当的版本。我不能不知道你的等级是什么样子。但是,请考虑你的设计:如果你有相同的命名操作给你完全不同的语义结果,操作是否正确命名/设计?< /P>你不能只使用不同的返回类型。方法签名是方法名称及其参数的数量和类型,这里两者都是相同的。谢谢。。。我必须更改我的函数名…const等修饰符也构成了方法signatureThaks的一部分!你提醒我我的设计是否适合我的需要
BoundingVolume
Geometry
应该相互区分,使用内部
Sphere
实现
BoundingSphere
更为合理。