C ++重载的函数问题 [英] C++ overloaded function issue

查看:121
本文介绍了C ++重载的函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么编译器找不到基类函数签名?正在将 foo(a1)更改为 B :: foo(a1)

Why does the compiler not find the base class function signature? Changing foo( a1 ) to B::foo( a1 ) works.

代码:

class A1 ;
class A2 ;

class B
{
public:
   void foo( A1* a1 ) { a1 = 0 ; }
} ;

class C : public B
{
public:
   void foo( A2* /*a2*/ )
   {
      A1* a1 = 0 ;
      foo( a1 ) ;
   }
} ;

int main()
{
   A2* a2 = 0 ;
   C c ;
   c.foo( a2 ) ;
   return 0 ;
}

编译器错误(VS2008):

Compiler error (VS2008):

error C2664: 'C::foo' : cannot convert parameter 1 from 'A1 *' to 'A2 *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


推荐答案

名称 C :: foo 隐藏名称 B :: foo 。一旦编译器在类B中找到匹配的 foo ,它将停止进一步搜索。

The name C::foo hides the name B::foo. Once the compiler finds the matching foo in class B, it stops searching any further.

您可以解决问题添加:

using B::foo;

添加到类C的主体或通过重命名类B中的函数。

to the body of class C, or by renaming the function in class B.

这篇关于C ++重载的函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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