473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Derived class losing base member function?

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
Jul 19 '05 #1
13 3699

"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
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.


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
Jul 19 '05 #2
> 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)?
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.


Because you are explicitly telling the compiler which function to use.
Jonathan
Jul 19 '05 #3
> > 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 :


Missed the virtual. See Mike's answer, I'm plain wrong.
Jonathan
Jul 19 '05 #4

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:xp*********************@weber.videotron.net.. .
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 :


Missed the virtual. See Mike's answer, I'm plain wrong.


Don't feel bad. I started writing a reply saying what you
did, but was lucky enough to catch myself before pressing
"send". :-)

-Mike
Jul 19 '05 #5
On Wed, 8 Oct 2003 00:17:47 -0400, "Jonathan Mcdougall"
<jo***************@DELyahoo.ca> wrote:
> 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 :


Missed the virtual. See Mike's answer, I'm plain wrong.


But you're right! The using declaration brings Base::Fn(int) into
derived member lookup scope, which is exactly what the OP wanted. The
virtual bit is irrelevent, since the OP still wants to call the most
overridden version of the function.

Why do you think you're wrong?

Tom
Jul 19 '05 #6
tom_usenet <to********@hotmail.com> wrote in message news:<5g********************************@4ax.com>. ..
On Wed, 8 Oct 2003 00:17:47 -0400, "Jonathan Mcdougall"
<jo***************@DELyahoo.ca> wrote:
> 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 :


Missed the virtual. See Mike's answer, I'm plain wrong.


But you're right! The using declaration brings Base::Fn(int) into
derived member lookup scope, which is exactly what the OP wanted. The
virtual bit is irrelevent, since the OP still wants to call the most
overridden version of the function.

Why do you think you're wrong?


Thanks Tom- adding the using declaration fixed the problem. Mike &
Jonathan- you guys didn't expect the using to work but it seems to do
exactly what I wanted. Is there something strange going on here?

-steve
Jul 19 '05 #7

"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
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.


Your "virtual" functions aren't really doing anything virtual because you're
not really using polymorphism. Try this
Base* d = new Derived;
d->Fn(5);
Jul 19 '05 #8

"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...

But you're right! The using declaration brings Base::Fn(int) into
derived member lookup scope, which is exactly what the OP wanted. The
virtual bit is irrelevent, since the OP still wants to call the most
overridden version of the function.

Why do you think you're wrong?


Thanks Tom- adding the using declaration fixed the problem. Mike &
Jonathan- you guys didn't expect the using to work but it seems to do
exactly what I wanted. Is there something strange going on here?


They're both right. The way you have the code written, the virtual
declarators are useless. It would do the same thing if you took them out.
On the other hand, even with them out, and even with a Derived object
defined as you have defined it, the call you want is now not in scope. So I
don't think this is "exactly what the OP wanted" as tom said. I think you
intended it to work polymorphically, or else you wouldn't have used
"virtual", right? So there are really 2 issues here.
Jul 19 '05 #9
"jeffc" <no****@nowhere.com> wrote in message news:<3f********@news1.prserv.net>...
"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
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.


Your "virtual" functions aren't really doing anything virtual because you're
not really using polymorphism. Try this
Base* d = new Derived;
d->Fn(5);


I understand in the chunk of code that I've posted, the virtual
keyword isn't doing anything. In other places I do work with base
class pointers, but I tried to keep the code to a minimum.

The using declaration that Jonathan suggested (then retracted) does
seem to work. Now I'm curious about why Jonathan retracted his
suggestion after he noticed the virtual keyword. What does that have
to do with anything?

-steve
Jul 19 '05 #10
On 8 Oct 2003 11:10:44 -0700, st***********@yahoo.com (Steve Canfield)
wrote:
I understand in the chunk of code that I've posted, the virtual
keyword isn't doing anything. In other places I do work with base
class pointers, but I tried to keep the code to a minimum.

The using declaration that Jonathan suggested (then retracted) does
seem to work. Now I'm curious about why Jonathan retracted his
suggestion after he noticed the virtual keyword. What does that have
to do with anything?


Nothing - the using declaration will only effect name lookup of
derived members (for which you need a derived reference/object).
Virtual dispatch from a Base reference will be completely unaffected.

Tom
Jul 19 '05 #11

"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...

Your "virtual" functions aren't really doing anything virtual because you're not really using polymorphism. Try this
Base* d = new Derived;
d->Fn(5);


I understand in the chunk of code that I've posted, the virtual
keyword isn't doing anything. In other places I do work with base
class pointers, but I tried to keep the code to a minimum.

The using declaration that Jonathan suggested (then retracted) does
seem to work. Now I'm curious about why Jonathan retracted his
suggestion after he noticed the virtual keyword. What does that have
to do with anything?


He was just a bit flummoxed by Mike's response at first. As I said, I think
there are really 2 issues here - both Jonathan and Mike were right. The
only question is which issue you were really interested in.
Jul 19 '05 #12
On 8 Oct 2003 11:10:44 -0700, st***********@yahoo.com (Steve Canfield)
wrote:
I understand in the chunk of code that I've posted, the virtual
keyword isn't doing anything. In other places I do work with base
class pointers, but I tried to keep the code to a minimum.

The using declaration that Jonathan suggested (then retracted) does
seem to work. Now I'm curious about why Jonathan retracted his
suggestion after he noticed the virtual keyword. What does that have
to do with anything?


On a final note:

http://www.research.att.com/~bs/bs_f...verloadderived

Tom
Jul 19 '05 #13
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<hS*****************@newsread3.news.pas.earth link.net>...
"Steve Canfield" <st***********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
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.


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. :-)


But the solution I stumbled upon isn't really the one I wanted. I much
perfer adding a 'using' declaration. That's what I did and it seems to
work.

Thanks for your help.
Jul 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
2
by: Luca | last post by:
Hi, I have a quite complex question to ask you: I have defined a base class where I would like to have a map holding pointers to member functions defined in derived classes. To be more precise...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
3
by: Bangalore | last post by:
Hi, In the following program, eventhogh two member function declared under private section of the derived class are accessable by derived class pointer. Please clarify me how can derived class...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
2
by: Fred | last post by:
I've got the following code: #include <iostream> class Base{ private: virtual void f(int) { std::cout << "f in Base" << std::endl; } };
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.