error: no matching function for call to , in static function - c++

I have a class named Book that contains an object from class Author. Book's constructor is a private field so I can not use the this class to define objects, so I have another class called BookBuilder which friend to Book and Author helps me to do this.
If I remove the static function problem disappears.
The problem is:
error: no matching function for call to 'Author :: Author ()
In fact I do not have the Author default constructor
class Book {
public:
friend class BookBuilder;
// ... other functions
private:
Book(const std::string title, const std::string publisher,const Author& author, const std::string genre, int isbn, double price):title_(title),publisher_(publisher),author_(author),isbn_(isbn),price_(price),genre_(genre){}
std::string title_;
std::string publisher_;
Author author_;
double price_;
int isbn_;
std::string genre_;
};
class BookBuilder {
public:
static BookBuilder start(){return BookBuilder();}
// ... other functions
private:
std::string title_;
std::string publisher_;
Author author_;
double price_;
int isbn_;
std::string genre_;
};

Your BookBuilder::start method is
static BookBuilder start(){ return BookBuilder(); }
This will try to return a default-constructed BookBuilder. This class has a member
Author author_;
So when you try to construct a BookBuilder, it will try to default initialize the members, including default initializing your auther_ in this case. Therefore Author needs a default constructor for this to work.

Related

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 define an Object using inheritance

I have a small problem with a task which was given to us for a preparation for my exam:
In the UML you see the classes. I have to define class Student.
The Constructor I have to define looks like this:
The class Students inherits from interface Immatrikulation and class person.
Also there is a struct for Adresse
In Adresse you can find the string ort (which I have to use in the Constructor) and in "Adresse" you have the string name(also needed)
Student(const string& name, const string& ort, int matrikelnr);
I know how I can access and save data for matrikelnr, since I have the variable in the same class, but i don't know how I can define ort and name for student.
UML
Just call the base class constructor. Following your UML:
class Student : public Person {
public:
Student(const string& name, const string& ort, int matrikeInr) :
Person(name, ort), matrikeINr(matrikeInr)
{ }
// etc...
private:
int matrikeINr;
};

How do I get private data from the member functions? c++

I am doing the Big c++ 2nd edition questions and I am on classes, I am stuck on a question. Basically I have to classes one called "person" and one called "Pemployee", I need to write the member functions of "Pemployee".
They gave me the declarations I need to implement the definitions.
What I don't get how to do is write call name, since it needs to call the private variable "person_data" which is a person object, I cannot access the string name directly but the member function of "person" has a "get_name" function which returns void, I dont understand how to get a string returned if I cannot return from the other definition.
Here are the two classes.
class Person
{
public:
Person();
Person(string pname, int page);
void get_name() const;
void get_age() const; //returns void
private:
string name;
int age; // 0 if unknown
};
class PEmployee
{
public:
PEmployee();
PEmployee(string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
string get_name() const; //assuming I need to call person get_name
private:
Person person_data;
double salary;
};
So once again, how do I call the "Pemployee" get_name to return the string name of the person_data private variable
Typo in the book, Person::get_name should return string
A lot of programming books teach you to add get and set methods to your classes when the variable needs to be read or modified from outside of the class.
You need to create get methods for each of the private variables that your want value access to outside of the class.
You will need a set methods if you want to change the values from outside the class.
Example get method for your class
std::string get_name() const {return this->name;}
These signatures in the class called Person are pointless:
void get_name() const;
void get_age() const;
It is very probable that it is a typo. If it returned string then it whouldn't be pointless.
However if you would not like to change the functions signature or add a new memeber to the Person, then you should declare PEmployee as friend of Person, so member functions of PEmployee can reach private and protected members of Person. You can see below how to do this.
#include <string>
class PEmployee;
class Person
{
public:
friend PEmployee;
Person(){ }
Person(std::string pname, int page);
void get_name() const;
void get_age() const; //returns void
private:
std::string name;
int age; // 0 if unknown
};
class PEmployee
{
public:
PEmployee();
PEmployee(std::string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
std::string get_name() const; //assuming I need to call person get_name
private:
Person person_data;
double salary;
};

error: no matching function for call to 'Employee::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.

How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

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