I'm having a bit of trouble figuring out this error I have. So I have a Person class, and a Student subclass.
The Person class has the following constructor:
Person(const string &name)
{ this->name = name;
}
The Student class has the following constructor:
Student::Student(const string &name, int regNo)
{ this->name = name;
this->regNo = regNo;
}
When I try to compile the Student class though, I get this error for the constructor:
In constructor 'Student::Student(const string&, int)':
error: no matching function for call to 'Person::Person()'
I'm new to C++, so I have no idea why I get this error, but it has something to do with the inheritance of Person obviously.
You are attempting to call Person's default constructor, but it doesn't have one. In any case, it looks like what you want to do is call its single parameter constructor:
Student::Student(const string& name, int regNo) : Person(name), regNo(regNo)
{
}
Similarly, use the constructor initialization list in Person's consructor:
Person(const string& name) : name(name) {}
This way, you are initializing your data members to the desired value, instead of default initializing them and then assigning them new values.
You need to delegate to the correct ctor from the base class like this:
Student::Student(const string &name, int regNo)
: Person( name )
{
this->regNo = regNo;
}
Note that this uses initializer lists, so you could use the more idiomatic
Student::Student(const string &name, int regNo)
: Person( name ), regNo( regNo )
{
}
and for the Person ctor:
Person(const string &name)
: name( name )
{
}
The initializer list is used to initialize base classes and the member variables, everything you don't explicitly put in there is default constructed (and in your code, it is later assigned in the ctor's body). Since Person is not default constructible, you must initialize it in the initializer list.
Related
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(){}
};
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.
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.
I am new to c++. I have been trying to get past this error. I know when a class in derived, it inherits everything from the base class, but what if the derived class has other data members? How is the constructor suppose to be?
When I try putting only the new I try to pass parameters to the newly made class members in the constructor I get an error to say it doesn't match that of the base class. When I try using that of the base class and adding the new data members it tells me I am redefining. So I wonder whats left to do to get past this error below is my code.
This is the base class:
class movielibrarybase
{
public:
movielibrarybase(string name, string dirname, string gen, int price);
void setname(string name);
string getname();
void setdirector_name(string dirname);
string getdirector_name();
void setgenre(string gen);
string getgenre();
void setprice(int price);
int getprice();
void display();
~movielibrarybase();
protected:
string name;
string director_name;
string genre;
int price;
};
And this is the derived class:
class songlibrary: public movielibrarybase
{
public:
songlibrary();
void setartist_name(string name);
string getartist_name();
void setsong_position(string position);
string getsong_position;
~songlibrary();
protected:
string artist_name;
string song_postion;
};
I am getting the following errors:
songlibrary.cpp||In constructor 'songlibrary::songlibrary(std::string, std::string, std::string, int, std::string, std::string)':|
songlibrary.cpp|6|error: no matching function for call to 'movielibrarybase::movielibrarybase()'|
movielibrarybase.cpp|3|note: candidates are: movielibrarybase::movielibrarybase(std::string, std::string, std::string, int)|
movielibrarybase.h|8|note: movielibrarybase::movielibrarybase(const movielibrarybase&)|
songlibrary.cpp|34|error: no 'std::string songlibrary::getsong_position()' member function declared in class 'songlibrary'|
For that case you use the member initializer list in the constructor to invoke the right constructor in the base class:
songlibrary::songlibrary(string name, string dirname, string gen, int price,
string artist_name, string song_position)
: movielibrarybase(name, dirname, gen, price), // initialize base class
artist_name(artist_name), // initialize member in this class
song_position(song_position) {
// other ctor stuff
}
If you do not state the parent class on that list, it will default to the default constructor. Since there is no default constructor in your movielibrarybase class, a compiler error occurred.
In your case it would be something like:-
songlibrary::songlibrary( string n, string director, string gen,
int pri, string artist, string song )
: movielibrarybase( n, director, gen, pri ),
artist_name( artist ), song_postion( song )
One thing to note here is sequence in member initializer list matters. First calls should be to base classes and then derived classes members should be initialized in the order in which they are declared in a class.
Also, class members are initialized in the order of their declaration in the class, the order in which they are listed in a member initialization list makes not a whit of difference
I have this class:
typedef int ID;
typedef int Salary;
class Worker {
protected:
ID id;
Salary& salary;
public:
Worker();
Worker(ID Id, Salary Salary) {
this->id=Id;
this->salary=salary;
}
.
.
.
}
and this one:
class WSalary : public Worker {
public:
WSalary(ID Id, Salary& Salary) {
this->id=Id;
this->salary=Salary;
}
.
.
.
}
and I have this error in the constructor
uninitialized reference member 'Worker::salary' [-fpermissive]
I want to put in the WSalary class a field (called salary) that has the same address in the field "salary" in the class worker.. so when I change the salary in the worker class it would change automatically in the WSalary class.
whats the solution?
thanks
In your worker example you are trying to bind a value to a non-const reference. This would create a dangling reference and is not a good idea. Just store a value in the class instead of a reference or pass a reference as the constructor argument and use the initialization list.
In WSalary you need to use the initialization list:
WSalary(ID id, Salary& s) : id(id), salaray(s) {}
Or call the constructor of the base (should you fix it to accept a reference):
WSalary(ID id, Salary& s) : Worker(id, s) {}
In C++11 you also just re-use the Worker constructor in the derived class using this syntax:
using Worker::Worker;
Use initialisation list:
WSalary(ID Id, Salary& Salary) : id(Id), salary(Salary)
{
}
instead of
WSalary(ID Id, Salary& Salary) {
this->id=Id;
this->salary=Salary;
}
If the indirection is intentional, then use a pointer.
A reference cannot be made to refer to something else, once it's been initialized.
This means that a reference member generally prohibits assignment for the class, and that it cannot be initialized by assignment.
Pointers, in contrast, can be made to point elsewhere by assignment.