派生类丢失基本成员函数? [英] Derived class losing base member function?
问题描述
编译以下代码时:
类底座
{
...
virtual void Fn(int x);
虚拟空虚Fn(基础x);
};
类派生:公共基地
{
...
虚拟空虚Fn(基础x);
};
派生d;
d.Fn(5);
我收到以下错误:
没有匹配函数来调用`Derived :: Fn(int)''
候选者是:void派生:: Fn(基础)
难道编译器(g ++)不能看到公共函数Base :: Fn(int)吗?
如果我帮助编译器,错误就会消失:
d .Base :: Fn(5)
但我不知道为什么我需要那样做。
-steve
When compiling the following code:
class Base
{
...
virtual void Fn(int x);
virtual void Fn(Base x);
};
class Derived : public Base
{
...
virtual void Fn(Base x);
};
Derived d;
d.Fn(5);
I get the following error:
no matching function for call to `Derived::Fn(int)''
candidates are: void Derived::Fn(Base)
Shouldn''t the compiler (g++) see the public function Base::Fn(int)?
The errors go away if I help the compiler out:
d.Base::Fn(5)
but I''m not sure why I need to do that.
-steve
推荐答案
" Steve Canfield" < ST *********** @ yahoo.com>在消息中写道
news:63 ************************** @ posting.google.c om ...
"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
编译以下代码时:
class Base
{
...
virtual void Fn(int x);
虚拟空虚Fn(基础x);
};
类派生:公共基础
{
...
虚拟空虚Fn(基础x);
};
派生d;
d.Fn(5);
我收到以下错误:
否调用`Derived :: Fn(int)''匹配函数的候选者是:void Derived :: Fn(Base)
不应该编译器(g ++)看到公众函数Base :: Fn(int)?
如果我帮助编译器错误就会消失:
d.Base :: Fn(5)
但我不知道为什么我需要这样做。
When compiling the following code:
class Base
{
...
virtual void Fn(int x);
virtual void Fn(Base x);
};
class Derived : public Base
{
...
virtual void Fn(Base x);
};
Derived d;
d.Fn(5);
I get the following error:
no matching function for call to `Derived::Fn(int)''
candidates are: void Derived::Fn(Base)
Shouldn''t the compiler (g++) see the public function Base::Fn(int)?
The errors go away if I help the compiler out:
d.Base::Fn(5)
but I''m not sure why I need to do that.
你没有覆盖基类中的''Fn(int)''。
既然你''没有进行多态调用(通过指针
或对ba的引用se class),只有类''Derived'的
成员函数才是名字查找的候选者。
类似的问题刚刚讨论过很久以前。
请参阅由Kris Thielemans发起的题为
调用被继承隐藏的虚函数的帖子。
你已经''绊倒''正确的解决方案。 :-)
-Mike
You didn''t override ''Fn(int)'' in the base class.
Since you''re not making a polymorphic call (via a pointer
or reference to the base class), only class ''Derived''s
member functions are candidates for name lookup.
A similar issue was just discussed a little while ago.
See the thread initiated by Kris Thielemans entitled
"calling virtual function that is hidden by inheritance".
You''ve already ''stumbled'' upon the correct solution. :-)
-Mike
>编译以下代码时:
> When compiling the following code:
class Base
{
...
virtual void Fn(int x);
虚拟空Fn(基础x);
};
类派生:公共基地
{
...
虚拟空虚Fn(基础x);
};
派生d;
d.Fn(5);
我收到以下错误:
没有匹配的呼叫功能到'Derived :: Fn(int)''
候选者是:void Derived :: Fn(Base)
不应该编译器(g ++)看到公共函数Base :: FN(INT)?
不。向派生类添加函数会隐藏具有相同名称的所有base'的
成员函数。你必须将它们带入
派生的范围:
class派生:公共基地
{
使用Base :: Fn;
...
};
出错如果我帮助编译器了:
d.Base :: Fn(5)
但我不知道为什么我需要这样做。
class Base
{
...
virtual void Fn(int x);
virtual void Fn(Base x);
};
class Derived : public Base
{
...
virtual void Fn(Base x);
};
Derived d;
d.Fn(5);
I get the following error:
no matching function for call to `Derived::Fn(int)''
candidates are: void Derived::Fn(Base)
Shouldn''t the compiler (g++) see the public function Base::Fn(int)?
Nope. Adding a function to a derived class hides all the base''s
member functions having the same name. You must bring them into
the derived''s scope :
class Derived : public Base
{
using Base::Fn;
...
};
The errors go away if I help the compiler out:
d.Base::Fn(5)
but I''m not sure why I need to do that.
>
因为您明确告诉编译器要使用哪个函数。
Jonathan
Because you are explicitly telling the compiler which function to use.
Jonathan
> >我收到以下错误:
> > I get the following error:
没有匹配函数来调用`Derived :: Fn(int)''
候选者是:void Derived :: Fn (基础)
不应该编译器(g ++)看到公共函数Base :: Fn(int)?
no matching function for call to `Derived::Fn(int)''
candidates are: void Derived::Fn(Base)
Shouldn''t the compiler (g++) see the public function Base::Fn(int)?
不。向派生类添加函数会隐藏具有相同名称的所有基本成员函数。你必须将它们带入
派生的范围:
Nope. Adding a function to a derived class hides all the base''s
member functions having the same name. You must bring them into
the derived''s scope :
错过虚拟。看到迈克的回答,我说错了。
Jonathan
Missed the virtual. See Mike''s answer, I''m plain wrong.
Jonathan
这篇关于派生类丢失基本成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!