不明确的类型引用 [英] Ambiguous type reference

查看:113
本文介绍了不明确的类型引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会这样:

template <typename T> 
struct foo
{
};

struct A
{
    typedef foo<A> type;
};

struct B : public A
{
    typedef foo<B> type;
};

int main()
{
    B::type john;
    return 0;
}

但不是这个:

template <typename T> 
struct foo
{
};

template <typename T>
struct Shared
{
    typedef foo<T> type;
};

struct A : public Shared<A>
{
};

struct B : public A, public Shared<B>
{
};

int main()
{
    // g++ 4.5 says :
    // error: reference to 'type' is ambiguous
    B::type john;
    return 0;
}

在我的代码中,foo 实际上是 boost::shared_ptr 并且,正如你所看到的,我正在尝试考虑一些 typedefs 使用 Shared 类.

In my code, foo is actually boost::shared_ptr and, as you can see, I'm trying so factor some typedefs using a Shared class.

推荐答案

因为 B 继承了 foo 并且间接地继承了 foo,并且都包含一个成员 type.你说的是哪个?

Because B inherits foo<B> and, indirectly, foo<A>, and both contain a member type. Which did you mean?

你简单的第一段代码有 Btype 隐藏 Atype,但这不会发生在更复杂的第二段代码中,它涉及更深的继承树.

Your simple, first piece of code has B's type hiding A's type, but that doesn't happen in the more complex second piece of code, which involves a deeper inheritance tree.

这篇关于不明确的类型引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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