473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Overriding an overloaded function

Let me try to describe the situation as clearly as I can.

In namespace XXX I have a class PARENT with the following public
functions:

virtual void foo( const char* stringBuf ); // set function for foo
virtual char* foo() const; // get function for foo

both of these functions have implementations in the PARENT.cpp file.

In namespace YYY I have a class CHILD

class CHILD: public XXX::PARENT
{
void foo( const char* stringBuff); // overrides parent's SET foo
// note that parent's GET foo is NOT overridden
}

CHILD.cpp has it's own foo implementation.

Along comes some class USER that uses CHILD.
YYY::CHILD* childPtr;
// some stuff that sets childPtr
someOtherString = childPtr->foo(); // should call the parent's GET

This line dies in the compiler.

"No matching function call to 'YYY::CHILD::fo o() const'
Candidates are: 'void YYY::CHILD::foo (const char *)'

Any idea why this fails to find the parent's inherited foo function?

By the way, there ARE other child classes, lets call them CHILD2 that
do NOT override either of the parent's foo functions. These classes
are used without difficulty.

Thanks for your help

Dave
Jul 22 '05 #1
3 2021


David Chandler wrote:

Any idea why this fails to find the parent's inherited foo function?


If you redefine a base classes function fred then you hide all other
functions with different signatures called fred in the base class.

Jul 22 '05 #2
On 2 Feb 2004 08:30:25 -0800 in comp.lang.c++, dc******@harris .com
(David Chandler) was alleged to have written:
"No matching function call to 'YYY::CHILD::fo o() const'
Candidates are: 'void YYY::CHILD::foo (const char *)'

Any idea why this fails to find the parent's inherited foo function?


It seems the error messages from recent compilers are less informative
here than some older compilers. Or, maybe you are not compiling with
the maximum warning level turned on?

Any function in the child class "hides" all functions of the same name
in the parent unless you bring them in with e.g.
using parent::foo;

For further discussion, See the topic "[23.6] What's the meaning of,
Warning: Derived::f(floa t) hides Base::f(int)?" in Marshall Cline's C++
FAQ, but discount Cline's alarmism on that subject. It is always good
to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Jul 22 '05 #3
David Chandler wrote:
In namespace XXX I have a class PARENT with the following public
functions:

virtual void foo( const char* stringBuf ); // set function for foo
virtual char* foo() const; // get function for foo
Those comments needed because the functions are poorly named. Overloading
get/set functions is cute, but it causes cognitive dissonance. Follow the
rule "name all functions with verb phrases".

Also, why is foo() returning a non-constant pointer? Users of foo() should
just tell PARENT what high-level thing they want to do to foo's data, and
let PARENT handle the details.
both of these functions have implementations in the PARENT.cpp file.

In namespace YYY I have a class CHILD

class CHILD: public XXX::PARENT
{
void foo( const char* stringBuff); // overrides parent's SET foo
// note that parent's GET foo is NOT overridden
}

CHILD.cpp has it's own foo implementation.

Along comes some class USER that uses CHILD.
YYY::CHILD* childPtr;
// some stuff that sets childPtr
someOtherString = childPtr->foo(); // should call the parent's GET

This line dies in the compiler.

"No matching function call to 'YYY::CHILD::fo o() const'
Candidates are: 'void YYY::CHILD::foo (const char *)'

Any idea why this fails to find the parent's inherited foo function?


Because void YYY::CHILD::foo (const char *) does not "overload" char *
XXX::PARENT::fo o(), it hides it. "Overload" means "same name within the same
scope". The lookup mechanics can only select overloads from one scope. So if
the compiler finds a near-match in one closer scope, it cannot look beyond
that scope to other scopes to find potential overloads.

Add "get" and "set" to your method names. Then try to take them out.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #4

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

Similar topics

2
3871
by: Generic Usenet Account | last post by:
The code example below fails to compile under g++ and Solaris native CC and gives the following output: "main.cc", line 12: Warning: derived::func hides the virtual function base::func(int). "main.cc", line 36: Error: Too few arguments in call to "derived::func(int, int)". 1 Error(s) and 1 Warning(s) detected. Why would overriding the...
1
2363
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms. Thanks. If giving more strategy hehind them, more helpful. Best Regards,
3
1698
by: N4M | last post by:
Dear, I have problems with overloaded operators ++() and --(). MSVC++ 6.0 compiler gives errors, one is shown as below: " c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++' : overriding virtual function differs from 'CGraphNodeIterI::++' only by return type or calling convention c:\data\c++\mygraphs\graph.h(141) : see...
3
7794
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
2
2432
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include...
15
23988
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a...
2
2013
by: byoukstetter | last post by:
So, I have an interface with several overriding methods: using System; using System.Collections.Specialized; namespace some.name.space { public interface IVrsPersistenceProvider { string Select(string idName, string tableName, string whereClause);
12
6713
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns True. Therefore, overriding the Equals method in the class definition of the items I put in the ArrayList should make the IndexOf method use my...
18
4715
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class...
3
2014
by: Rick | last post by:
One of the rules I have recommended for our new coding standard is as follows: "When overriding a base class virtual function, all overloads of that base class virtual function should also be overridden. Otherwise, the overloads of the overridden function in the base class will not be visible from the derived class." In other words...
0
7479
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.