C++,vs 2010 中的不明确继承错误
我在这个例子中应用多态性时遇到了一些麻烦。这个问题与我的上一个问题类似
有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Visual C++ 中的一个已知错误:
您需要消除协方差或虚拟继承。不幸的是,你不能两者兼得。
This is a known bug in Visual C++:
You either need to eliminate the covariance or the virtual inheritance. Unfortunately, you can't have both.