C++,vs 2010 中的模糊继承错误 [英] C++, ambiguous inheritance error in vs 2010

查看:39
本文介绍了C++,vs 2010 中的模糊继承错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个例子中对多态的应用有一些麻烦.这个问题和我上一个问题类似

I have some troubles with the application of polymorphism in this example. This question is similar to my last question

C++,虚继承,奇怪的抽象类+克隆问题

有3个抽象类:

class A
{
public:
    virtual A  * copy () const = 0;
    virtual ~A() = 0;
};

A::~A(){}

class B
{
public:
    virtual B  * copy () const = 0;
    virtual ~B() = 0;
};

B::~B(){}

class C: virtual public A , public B 
{
public:
    virtual C  * copy () const = 0;
    virtual ~C() = 0;
};

C::~C(){}

和两个使用虚拟继承的继承类

and two inherited classes using the virtual inheritance

class D: virtual public A
{
public:
    virtual D  * copy () const {return new D  (*this);}
    virtual ~D() {}
};

class E: virtual public D , public C
{
public:
    virtual E * copy () const {return new E (*this);}
    virtual ~E() {}
}; //Error C2250: 'E' : ambiguous inheritance of 'D *A::copy(void) const

上述错误仅在使用 MSVS 2010 编译器时发生,g++ 编译此代码正常.

The above mentioned error occurs only using MSVS 2010 compiler, g++ compiles this code OK.

类图(简化)

.......... A .... B.....
........../.\..../......
........./...\../.......
......../.....\/........
.......D...... C........
........\...../.........
.........\.../..........
..........\./...........
...........E............

最后的讨论我们以结果结束:从类 C 中删除 copy() 方法的声明.

Last discussion we close with the result: remove the declaration of the copy() method from class C.

class C: virtual public A , public B 
{
public:
    //virtual C  * copy () const = 0; //remove declaration
    virtual ~C() = 0;
};

C::~C(){}

我使用多态的示例代码需要创建指向 C 的指针向量.删除一些元素后我想创建它的副本...我需要在类 C 中声明 copy(),因此删除声明是不够的它没有解决问题.

My sample code using polymorphism needs to create vector of pointers to C. After removing some element I want to create its copy... I NEED a declaration of copy() in class C, so removal of the declaration is insufficient and it does not solve the problem.

int main(int argc, char* argv[])
{
std::vector <C*> items;
items.push_back(new E());
items.push_back(new E());
items[0]->copy();
return 0;
}

你能帮我吗,请问如何更正代码以使用 VS 2010 进行翻译?

Could you help me, please how to correct the code to be translatable using VS 2010?

推荐答案

这是 Visual C++ 中的已知错误:

This is a known bug in Visual C++:

Visual C++ 在协方​​差与虚拟继承一起使用时错误地报告歧义

您要么需要消除协方差,要么需要消除虚拟继承.不幸的是,您不能两者兼得.

You either need to eliminate the covariance or the virtual inheritance. Unfortunately, you can't have both.

这篇关于C++,vs 2010 中的模糊继承错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆