I have a base class that has constructor Person(string name, int age)
and a derived class Empolyee(string job_title, int salary)
When I try to call it using new Employee(name, age, job_title, salary) the
compiler tells me no matching function for call to `Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary)
{
Person(name, age);
Title=job_title;
Wage=salary;
}
Could someone tell me what I am doing wrong? 3 2671
"James" <a@a.a> wrote in message news:43********@dnews.tpgi.com.au... I have a base class that has constructor Person(string name, int age) and a derived class Empolyee(string job_title, int salary)
When I try to call it using new Employee(name, age, job_title, salary) the compiler tells me no matching function for call to `Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary) { Person(name, age); Title=job_title; Wage=salary; }
Could someone tell me what I am doing wrong?
You should grab a C++ book and read about initialization lists:
Employee::Employee(string name, int age, string job_title, int salary)
: Person(name,age)
, Title(job_title)
, Wage(salary)
{ }
I hope this helps,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
if you have
class person
{
int ...
string ...
public:
person(int, string);
};
class Empoyee: public person
{
int ...
String...
public:
Employee(int ,string,int, string)
{
Person(int ,string);
}
}
every derived object has a unamed contained object of the base class.
so it will try to call the default constructor first and then the parameterized constructor.
to avoid this either do
class Empoyee: public person
{
int ...
String...
public:
Employee(int ,string,int, string): Person(int ,string)//member initializer list
{
}
}
or provide a copy of default c'tor in the person class along with the parameterized.
"James" <a@a.a> wrote in message news:43********@dnews.tpgi.com.au I have a base class that has constructor Person(string name, int age) and a derived class Empolyee(string job_title, int salary)
When I try to call it using new Employee(name, age, job_title, salary) the compiler tells me no matching function for call to `Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary) { Person(name, age); Title=job_title; Wage=salary; }
Could someone tell me what I am doing wrong?
Yes. Base constructors must be called in the initialisation list:
Employee::Employee(string name, int age, string job_title, int salary) :
Person(name, age)
{
Title=job_title;
Wage=salary;
}
When you omit the Base constructor from the initialisation list, the default
constructor of Person() is called. Since you don't have a default
constructor, you get the error.
When you call Person(name, age); in the body of the Employee constructor,
this does *not* call the Base constructor. Instead, it creates a Person
temporary, which disappears immediately.
--
John Carson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fernando Rodriguez |
last post by:
Hi,
I'm having trouble with a metaclass suposed to check the method signature of
its classes.
Here's the metaclass:
class MetaChecker(type):
def __new__(cls, name, bases, attribs):
for...
|
by: Brent |
last post by:
I'd like to subclass the built-in str type. For example:
--
class MyString(str):
def __init__(self, txt, data):
super(MyString,self).__init__(txt)
self.data = data
|
by: JKop |
last post by:
I'm starting to think that whenever you derive one class from another, that
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that...
|
by: zippy747 |
last post by:
I would like to have a base class that does some work and can be
instantiated.
class a
{
public:
virtual int foo() { return 0; };
virtual int bar() { return 0; };
};
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Larry Serflaten |
last post by:
I am trying to build a chart to help me track what gets called when
in the inheritance chain. I came across an error that has me puzzled.
I have a derived class that inherits from a base class,...
|
by: Nathan Sokalski |
last post by:
I am having trouble removing cookies that I created with my site. Here is
the code I am using to try and remove them:
If Not Request.Cookies("username") Is Nothing Then...
|
by: ingoweiss |
last post by:
Hi,
I am having trouble passing parameters of a Javascript subclass
constructor through to it's superclass constructor.
I am trying all sorts of things, including the below, but nothing...
|
by: rossabri |
last post by:
This topic has been addressed in limited detail in other threads:
"sockets don't play nice with new style classes :(" May 14 2005....
|
by: dylan.miller |
last post by:
I'm having trouble understanding the internal access modifier. There
are many classes in my assembly that should not be accessible outside
of the assembly. I've used the internal access modifier...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |