error: no matching function for call to 'Employee::Employee()' - c++

Looked at similar threads and this doesn't show up. Basically I want chef to inherit the functions and data from employee (base class) but i'm having issues with the constructor for the derived class. I'm getting the error: no matching function for call to 'Employee::Employee()' Could someone show me how to declare my constructors for this derived class and my future derived classes for this program. Tried a bunch of things and can't seem to get it working.
class Employee
{
public:
Employee(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary)
{
this->empID = theempID;
this->firstName = thefirstName;
this->lastName = thelastName;
this->empClass = theempClass;
this->salary = thesalary;
};
protected:
int empID;
string firstName;
string lastName;
char empClass;
int salary;
};
class Chef : public Employee
{
public:
Chef(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary, string theempCuisine) : Employee() {}
{
this->empID = theempID;
this->firstName = thefirstName;
this->lastName = thelastName;
this->empClass = theempClass;
this->salary = thesalary;
this->empCuisine = theempCuisine;
};
string getCuisine()
{
return empCuisine;
}
protected:
string empCuisine;
};
#endif // EMPLOYEE

Employee() is trying to default construct an Employee but there is no default constructor for Employee. Instead, construct it with the parameters your constructor it expects.
The Chef constructor should look like this:
Chef(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary, string theempCuisine) :
Employee(theempID, thefirstName, thelastName, theempClass, thesalary), empCuisine(theempCuisine)
{}
Note the body of the constructor is empty. The Employee base class and the member variable are initialized in the initialization list. No assignment necessary in the body. You should also change the base class constructor so it uses initialization instead of assignment.

Related

Explicitly initalize abstract base class constructor with value determined by parameter of derived class constructor

Within my vehicle base class, I have a private member variable, string type (for type of vehicle, ie car, motorbike, tricycle, etc).
#pragma once
using namespace std;
#include <string>
#include <iostream>
class vehicle {
public:
vehicle(string reg, string make, string model, int age, string type);
virtual ~vehicle() = default;
virtual double costPerDay() = 0;
protected:
int age;
int perDayCostCap(int costPD);
double penceToPounds(int pence);
private:
const string type;
string const reg, make, model;
};
One of the derived classes, bike, has a numberOfWheels variable which is to be passed into its constructor. I want to initialize the base class constructor with type bicycle or tricycle depending on the numberOfWheels.
I can not figure out how to achieve this, seeing as the base class constructor has to be initialized before the function body of the child class.
The following shows what I would like to achieve (though, I know this is not possible):
bike::bike(int engineCC, int numOfWheels, string reg, string make, string model, int age)
:engineCC(engineCC), numOfWheels(numOfWheels) {
string tricOrBic = (numOfWheels == 2) ? "bicicle" : "tricicle";
vehicle:reg=reg, make=make, model=model, age=age, type=tricOrBic;
};
Like this?
bike::bike(int engineCC, int numOfWheels, string reg, string make, string model, int age)
: vehicle(reg, make, model, age, numOfWheels == 2 ? "bicycle" : "tricycle")
, engineCC(engineCC)
, numOfWheels(numOfWheels)
{
}
This is normal programming, maybe you had some problem I'm not seeing.

C++ Declaring an inherited constructor?

I'm having difficulties in defining a constructor for a class that inherits the properties of another class
class Transportation {
public:
int ID;
string company;
string vehicleOperator;
Transportation(int,string,string) {
}
};
class SeaTransport: public Transportation {
public:
int portNumber;
SeaTransport(int)::Transportation(int,string,string) {
}
};
I'm having issues with line 18 (SeaTransport(int)::Transportation(int,string,string)).
The error I receive occurs at the pont where I declare Transportation.
As seen in the code, a class Transportation is the body class and class SeaTransport inherits the properies of Transportation.
Transportation::Transportation(int, std::string, std::string)
+2 overloads
type name is not allowed
This error occurs at the int
typedef std::__cxx11::basic_string std::string
type name is not allowed
and this final error occurs at both string variables.
It seems you mix together scoping and a constructor initializer list.
The double-colon operator :: is for scope, while a constructor followed by a single colon and a list of initializations is an initializer list.
You must declare the SeaTransport constructor to take all the arguments, including those for the parent class (assuming you want to pass them on to the base constructor):
SeaTransport(int port, int id, string company, string operator);
Then in the definition (implementation) of the constructor you "call" the parent constructor in the constructor initializer list:
SeaTransport(int port, int id, string company, string oper)
: Transport(id, company, oper), // "Call" the parent class constructor
portNumber(port) // Initialize the own members
{
}
As Mr Some Programmer Dude said, you've a Scope problem in your code,
I will try to answer for your second question which is, how to add featured variables on your constructor.
Same as what you did for the port attribute.
You can define before all your Attribute which is boatNumber as int boadNumber = 0 then, you'll overload your
constructor with boatNumber(num) after the initializer operator and int num before the initializer operator.
class Transportation {
public:
int ID;
string company;
string vehicleOperator;
Transportation(int,string,string) {
}
~Transportation(){}
};
class SeaTransport: public Transportation {
public:
int portNumber;
int boatNumber;
SeaTransport(int num, int port, int id, string company, string oper)
:Transportation(id, company, oper), boatNumber(num),portNumber(port) {}
~SeaTransport(){}
};
But, if you want to get things more specific, you can create another class which is derived from SeaTransport
And then you'll define the number of your boat and more other details, if you want.
I'll draw you an instance of it :
class Boat: public SeaTransport {
public:
int boatNumber;
Boat(int bNum,int num, int port, int id, string company, string oper):
SeaTransport( num, port, id, company, oper),boatNumber(bNum){}
~Boat(){}
};

How to inherit from two classes and call the constructor of the parent class I need to make an object of?

Hello I am fairly new in C++ and I am trying to make a programm that you have objects of Application class.
Application class should inherit from two classes. The class Games and the class desktopApp.
When I build my programm I get the two followin errors:
no matching function for call to 'desktopApp::desktopApp()
no matching function for call to 'Games::Games()
My questions are:
Why I get these errors?
How can I call the constructor of the parent class I want to make an object of everytime?
Do I need to make two constructors one for each parent class in Application class?
Thank you very much.
Games class code:
class Games
{
string category;
float price;
public:
Games (string category, float price)
{
this->category = category;
this->price = price;
}
};
desktopApp class code:
class desktopApp
{
string edition;
vector<string> ratings;
public:
desktopApp (string edition, vector<string> ratings)
{
this->edition = edition;
copy(this->ratings.begin(), this->ratings.end(), back_inserter(ratings));
}
};
Application class code:
class Application:public desktopApp, public Games
{
string name;
public:
Application (string name, string category, float price):Games (category, price)
{
this->name = name;
}
Application (string name, string edition, vector<string> ratings):desktopApp (edition, ratings)
{
this->name = name;
}
};
Main:
int main()
{
Applications game1("aGame", "Violent", 45.7);
}
Having two base classes A and B, a class C that inherits from both, looks like this:
class C : public A, public B{
public:
C(/*params*/) : A(/*params*/), B(/*params*/){/*code*/}
};
So in your case your Application should looks like this:
class Application:public desktopApp, public Games
{
string name;
public:
Application(string edition, vector<string> ratings, string name, string category, float price) :
desktopApp(edition,ratings),
Games(category,price)
{
this->name = name;
}
Application(string category, float price, string name, string edition, vector<string> ratings) :
Games(category,price),
desktopApp(edition,ratings)
{
this->name = name;
}
};

Child Class constructor using private member

I have multiple child classes extending the base class (Media).
When I call the child classses constructor, I am not able to use the private member mBookLateFee's value to do some calculations .. it appears to be using the default 0.0 and not the members contents.
However, if I put the actual value of 1.25 in there it works..
When constructed can the object not initialize from a child classes member?
class Media {
private :
string mCallNumber;
protected:
string mStatus;
string mTitle;
string mType;
int mDaysOverDue = 0;
double mDailyLateFee = 0.0;
double mTotalLateFees = 0.0;
public:
Media(string status, string title, int days=0, string callNum="", string type="", double fee=0.0) : mStatus(status),
mTitle(title), mDaysOverDue(days), mCallNumber(callNum), mType(type), mDailyLateFee(fee) {};
~Media(){};
void setCallNo(string newCallNum);
string getCallNo();
void setHowLate(int numDays);
int getDaysOverDue();
virtual void print();
virtual double getFees() = 0;
};
class Book : public Media {
private:
double mBookLateFee = 1.25;
protected:
string mAuthor;
int mNumPages;
public:
Book(string status, string title, int days, string callNum, string author, int pages=0) :
Media(status, title, days, callNum, "Book", mBookLateFee), mAuthor(author), mNumPages(pages){
mTotalLateFees = mDaysOverDue * mDailyLateFee;
};
double getFees() { return mTotalLateFees;}
void print();
};
The superclass, the parent class, gets constructed before the child class.
When your child class invokes the superclass's, the parent class's constructor, the child class has not been constructed yet. It's mBooklateFee is not yet constructed.
In a manner of speaking, it does not exist yet. Hence, you cannot use it in order to invoke the parent class's constructor.

class inheritance code not working

I have checked the class inheritance syntax and I'm pretty sure I don’t have a mistake ? did anything slip away from me ?
class person
{
public:
int personid;
string personname;
string personadress;
person( int apersonid, string apersonname, string apersonadress )//constructor
{
personid=apersonid;
personname=apersonname;
personadress=apersonadress;
}
int getpersonid()
{
return personid;
}
string getpersonname()
{
return personname;
}
string getpersonadress()
{
return personadress;
}
};
class employee: public person
{
public:
int commission;
employee(int _commission, int apersonid, string apersonname, string apersonaddress) : person(apersonid, apersonname, apersonaddress)
{
commission= _commission;
}
int getcommission()
{
return commission;
}
};
The error I'm getting is
Error 1 error C2512: 'person' : no appropriate default constructor
available
I suspect your problem is that your base class doesn't define a default constructor (with 0 arguments). Instead you have a constructor with multiple arguments.
person( int apersonid, string apersonname, string apersonadress )//constructor
{
personid=apersonid;
personname=apersonname;
personadress=apersonadress;
}
Your employee class inherits from person but it's constructor only takes one argument and does not initialize the base class using it's constructor.
employee( int _commission )
{
commission= _commission;
}
This is an issue b/c when you create an instance of the employee, it's trying to call the person class constructor as well. Since you don't have a default constructor (again, no arguments), you will need to explicitly call the person constructor in the employee constructor.
You have two options.
First, Modify the person constructor to take no arguments
person()
If you use this method, your signature for your employee class won't have to change.
Or you can Modify the employee constructor to take all the arguments the person constructor expects:
class employee: public person
{
public:
int commission;
employee(int _commision, int apersonid, string apersonname, string apersonadress ) : person(apersonId, apersonname, apersonadress)
{
commission = _commission;
}
int getcommission()
{
return commission;
}
};
In the constructor, the code after the colon (:) is a call to the person constructor using the arguments specified (apersonid, apersonname, apersonadress).
The employee constructor then assigns the value of _commission since it's scope is only relevant within the employee class.
Does that make sense?
In C++, if constructors are explicitly defined for a class, but they are all non-default, the compiler will not implicitly define a default constructor, leading to a situation where the class does not have a default constructor. You have defined a non default constructor in your class, this one :
person( int apersonid, string apersonname, string apersonadress )
So you need to define one default constructor yourself. E.g.
person() {}
Keep in mind, that in this case, when you construct an object of employee class the default constructor of person would be called. If that is not desired you need to call the non-default constructor of person in the constructor of employee.