4

尝试编译代码时出现编译错误。错误是这样的:

multi.cc: In function ‘int main()’:
multi.cc:35: error: cannot declare variable ‘mdc’ to be of abstract type ‘MostDerivedClass’
multi.cc:27: note:   because the following virtual functions are pure within ‘MostDerivedClass’:
multi.cc:13: note:  virtual int Interface2::common_func()
multi.cc:36: error: request for member ‘common_func’ is ambiguous
multi.cc:13: error: candidates are: virtual int Interface2::common_func()
multi.cc:21: error:                 virtual int InterimClass::common_func()

这是我的代码:

class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class Interface2 {
public:
    virtual int common_func() = 0;
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};


class InterimClass : public Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};


class MostDerivedClass : public InterimClass, public Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};

int main() {
    MostDerivedClass mdc;
    int x = mdc.common_func();
    cout << "The value = " << x << endl;    

    Interface2 &subset_of_funcs = dynamic_cast<Interface2 &>(mdc);
    x = subset_of_funcs.common_func();
}

我的问题:

  • 如何告诉编译器 common_func() 已经由作为 MostDerivedClass 的基类的 InterimClass 实现?

  • 还有其他方法可以解决我的问题吗?我真正想做的是能够从 Interface2 调用 common_func。我正在使用 Interface1 中包含大量方法的一些遗留代码。在我的新代码中,我只想调用一小组这些 Interface1 函数,以及一些我需要添加的函数。

4

4 回答 4

4

您需要定义一个common_func()无论如何MostDerivedClass以满足您的继承Interface2

你可以尝试类似的东西

virtual int common_func() {
    return InterimClass::common_func();
}

如果您无法更改第一个,这是最有用的Interface1

如果你想在你的类之间建立真正的继承关系,你需要遵循 Lol4t0 的建议。从 中提取一个超类Interface1,并Interface2创建这个新创建的类的子类。例子 :

class RootInterface{
public :
    virtual int common_func() = 0;
    virtual ~RootInterface(){}
};

class Interface1 : public virtual RootInterface{
public:
    virtual ~Interface1() {};
};

class Interface2 : public virtual RootInterface{
    public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class InterimClass : public Interface1 {
    public:
    virtual int common_func() {
        return 10;
    }
};

class MostDerivedClass : public InterimClass, public Interface2 {
public:
    virtual int new_func() {
        return 20;
    }
};
于 2012-08-12T19:02:49.847 回答
0

在 MostDerivedClass 中添加一个覆盖,并从中调用 InterimClass::common_func()。

于 2012-08-12T19:03:22.573 回答
0

首先,我不太了解您的代码的含义。

您需要知道只实现了 Interface1::common_func。

为什么不让 Interface2 从 Interface1 继承?我猜你希望两个 common_func 方法是相等的。

示例代码(使用多态性):

class Interface1 
{
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class Interface2 : public Interface1 {
public:
    virtual int common_func() = 0;
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class InterimClass : public Interface2 {
    public:
        virtual int common_func() {
            return 10;
        }
};

class MostDerivedClass : public InterimClass {
public:
    virtual int new_func() {
        return 20;
    }
};

int test_func()
{
    Interface1 * i1 = new MostDerivedClass;
    int x = i1->common_func();
    cout << "The value = " << x << endl;

    Interface2 * i2 = new MostDerivedClass;
    x = i2->common_func();

    return 0;
}
于 2012-08-12T19:14:02.727 回答
0

让第二个接口派生自第一个接口,去掉virtual int common_func() = 0;第二个接口的声明,&使用关键字virtual来引导编译器实现。

class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class BaseClass : public virtual Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};

class Interface2 : public virtual Interface1{
public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class DerivedClass : public virtual BaseClass, public virtual Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};
于 2015-06-26T20:25:48.967 回答