Fixing C++ Multiple Inheritance Ambiguous Call

穿精又带淫゛_ 提交于 2019-12-05 07:58:41

This is because of a diamond inheritance pattern, to resolve the error you can specify the specific namespace you want the member from like.

paddressType->MeasurementKeyword::GetValue()

or

paddressType->CharacterKeyword::GetValue()  

  1. Basically your AddressType class has access to the GetValue members from both the classes it inherits from and can't choose one (call is ambiguous).
  2. The scope resolution operator (:: ) helps specify which one you actually want.
  3. You haven't said what you actually want this code to do so I'll just say that generally complex inheritance patterns are not conducive to creating readable code, rethink what you actually want.

define AddressType like this:

class AddressType : public CharacterKeyword, public MeasurementKeyword
{
public:
    using MeasurementKeyword::GetValue;
private:
    float address;
    float addresExt;
};

Generally, when you run into the deadly diamond of death it is a sign that you should rethink your design. However, if you absolutely cannot avoid this situation, C++ provides a solution in the form of virtual inheritance. Virtual inheritance resolves some of the "diamond ambiguities", but it is also clunky. For example, you have to explicitly call the parent's non-default constructors in the derived class' constructor.

Once again, the best way is to avoid the diamond in the first place. I have been programming in C++ for many years, and so far I have never had this problem in my code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!