LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2013, 10:26 PM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
Overload method problem in Derived class.


Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <memory>
using namespace std;
class CFather
{
public:
    void DoWork(int val)
    {   
    }   
};
class CSon : public CFather
{
public:
    void DoWork(int val, int two)
    {   
    }   
};
int main(void)
{
    CSon* myson = new CSon;
    myson->DoWork(12);
    delete myson;
}
When I compile the code above, g++ complains:
derived_test.cpp:24: error: no matching function for call to 'CSon:: DoWork(int)'
derived_test.cpp:17: note: candidates are: void CSon:: DoWork(int, int)
I don't get the reason and how to solve this problem, Any advice would be kind, Thanks in advance.
 
Old 03-27-2013, 11:49 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

CFather:oWork(int) has been hidden by CSon:oWork(int,int).
You can overcome this by adding a "using" statement to the CSon. Eg
Code:
class CSon : public CFather
{
public:
    using CFather::DoWork;
    void DoWork(int val, int two)
    {   
    }   
};
HTH,

Evo2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can't we access a protected member in a derived class by a base class's object? Aquarius_Girl Programming 5 02-04-2012 10:35 PM
[SOLVED] Python n00b: promoting a derived class to base class? CoderMan Programming 2 03-11-2010 01:46 PM
C++: derived class? unihiekka Programming 4 02-03-2009 09:40 AM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration