具有多重继承的operator()的含糊定义 [英] Ambiguous definition of operator() with multiple inheritance

查看:100
本文介绍了具有多重继承的operator()的含糊定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用GCC(4.2.1 Apple build 5664)编译此代码

  #include< cstddef& 

使用std :: size_t;

template< char I> struct index {};

struct a
{
void operator()(size_t const&){}
};

struct b
{
template< char I>
void operator()(index< I> const&){}
};

struct c:public a,public b {};

int main(int argc,char const * argv [])
{
c vc;
vc(1);

return 0;
}

并给出以下错误:

  main.cpp:在函数'int main(int,const char **)':
main.cpp:22:error: operator()'是不明确的
main.cpp:14:error:candidates are:template< char I> void b :: operator()(const index< I&)
main.cpp:9:error:void a :: operator()(const size_t&)

我不明白为什么这个代码不明确的原因; c> c c>



  struct c:public a,public b 
{
using a :: operator
using b :: operator();
};

C ++(在C ++ 0x之前)在继承函数中有点尴尬:



看起来像继承自两个类也有同样的问题。



//寻找标准...


I compile this code with GCC (4.2.1 Apple build 5664)

#include <cstddef>

using std::size_t;

template <char I> struct index { };

struct a
{
    void operator()(size_t const &) { }
};

struct b
{
    template <char I>
    void operator()(index<I> const &) { }
};

struct c: public a, public b { };

int main (int argc, char const *argv[])
{
    c vc;
    vc(1);

    return 0;
}

and give me the following error:

main.cpp: In function ‘int main(int, const char**)’:
main.cpp:22: error: request for member ‘operator()’ is ambiguous
main.cpp:14: error: candidates are: template<char I> void b::operator()(const index<I>&)
main.cpp:9:  error:                 void a::operator()(const size_t&)

I don't understand the reason why this code is ambiguous; the two methods have different signatures.

解决方案

Modify c this way:

struct c: public a, public b
{
    using a::operator();
    using b::operator();
};

C++ (prior to C++0x) is kind of awkward in inheriting functions: if you provide a function with the same name of a base class' function it hides base class ones.

It looks like also inheriting from two classes has the same problem.

// looking for the standard...

这篇关于具有多重继承的operator()的含糊定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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