C++,vs 2010 中的不明确继承错误

发布于 2024-11-27 12:59:50 字数 1730 浏览 4 评论 0原文

我在这个例子中应用多态性时遇到了一些麻烦。这个问题与我的上一个问题类似

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(){}

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

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++编译此代码正常。

类图(简化)

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

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

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

C::~C(){}

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

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 进行翻译吗?

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

C++, virtual inheritance, strange abstract class + clone problem

There are 3 abstract classes:

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

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

Class diagram (simplified)

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

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(){}

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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-12-04 12:59:50

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

当协变与虚拟继承一起使用时,Visual C++ 会错误地报告歧义

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

This is a known bug in Visual C++:

Visual C++ incorrectly reports ambiguity when covariance is used with virtual inheritance

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文