修复C ++多继承模糊调用 [英] Fixing C++ Multiple Inheritance Ambiguous Call

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

问题描述

我有三个类结构如下:

#include <iostream>
using namespace std;

class Keyword
{
    public:
        virtual float GetValue() = 0;
};

class CharacterKeyword : public Keyword
{
    public:
        virtual float GetValue(){return _value;}
    private:
        float _value;
};

class MeasurementKeyword : public Keyword
{
    public:
        virtual float GetValue(){return _value;}
    private:
        float _value;
};

class AddressType : public CharacterKeyword, public MeasurementKeyword
{

    private:
        float address;
        float addresExt;
};

int main()
{
    AddressType *a = new AddressType();
    a->GetValue();
    return 0;
}

我得到以下结果:


在函数'int main()':

错误:请求成员'GetValue' code>

错误:candidate are:virtual float关键字:: GetValue()

错误:virtual float CharacterKeyword :: GetValue()

错误:virtual float CharacterKeyword :: GetValue >

I am getting the following:

In function ‘int main()’:
error: request for member ‘GetValue’ is ambiguous
error: candidates are: virtual float Keyword::GetValue()
error: virtual float MeasurementKeyword::GetValue()
error: virtual float CharacterKeyword::GetValue()

我已经读过多个继承,我知道它有很多陷阱 - 这是其中之一。我需要我的类结构是这样的,所以我想知道是否有一种方法,我可以解决这个使用模板?

I have done some reading into multiple inheritance and I know that it has a lot of pitfalls - this being one of them. I need my class structure to be like this so I was wondering if there was a way that I could fix this using templates?

更新

在阅读您的评论后,我原来的想法是,也许我可以在 AddressType ,即 CharacterKeyword AddressType c $ c> AddressType
的模板 MeasurementKeyword
。并在更新的代码中使用它。 我可以只指定我想要的成员的命名空间。因为模板方式还没有被提及作为答案,这是一个坏的解决方案吗?我应该只指定我想要的成员的命名空间。

Update
After reading your comments, my original thought was that maybe I can just delineate between an AddressType that is a CharacterKeyword and an AddressType that is a MeasurementKeyword by templating the AddressType. And using it as such in the updated code. OR I can just specify the namespace of the member that I would like. Since the templated way has not been mentioned yet as an answer, is it a bad fix? Should I just specify the namespace of the member I want?

template <class T>
class AddressType : public T
{

    private:
        float address;
        float addresExt;
};

int main()
{
    AddressType<MeasurementKeyword> *a = new AddressType<MeasurementKeyword>();
    a->GetValue();
    return 0;
}


推荐答案

a href =http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem>钻石继承模式,要解决错误,您可以指定您希望成员从哪个特定的命名空间。

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

paddressType->CharacterKeyword::GetValue()  








  1. $ c> AddressType 类可以从它继承的两个类中访问 GetValue 成员,并且不能选择一个(调用是不明确的)。

  2. 范围解析运算符( :: 有助于指定您实际需要哪一个。

  3. 你没有说你真正想要的代码这样做,我只是说通常复杂的继承模式不利于创建可读的代码,重新思考你实际想要的。 / li>
  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.

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

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