I am trying to understand the error I am receiving:
error: is implicitly deleted because the default definition would be ill-formed
Looking into why I am getting the error, I believe it has something to do with not having a default constructor for class Dealer. I'm not quite understanding why that is the case in regards to this program. I believe it has to do with the Motorcycle m_motorcycle object I created.
#include <iostream>
using namespace std;
class Motorcycle{
public:
string m_make;
string m_model;
int m_year;
Motorcycle(string, string, int);
};
class Dealer{
private:
Motorcycle m_motorcycles[3];
public:
void addMotorcycle(Motorcycle);
Motorcycle getMotorcycle(int);
};
int main(){
Motorcycle bike1("Ductai", "Scrambler", 2020);
Dealer dealer1;
dealer1.addMotorcycle(bike1);
return 0;
}
Motorcycle::Motorcycle(string make, string model, int year){
this->m_make = make;
this->m_model = model;
this->m_year = year;
}
void Dealer::addMotorcycle(Motorcycle bike){
this->m_motorcycles[0] = bike;
}
The error is saying that implicit default constructor of class Dealer is deleted since it contains Motorcycle m_motorcycles[3];, and Motorcycle has no default constructor.
A way to solve this is to include std::vector<Motorcycle> m_motorcycles; in class Dealer instead of the array, and also change Dealer::addMotorcycle():
void Dealer::addMotorcycle(Motorcycle bike){
this->m_motorcycles.push_back( bike );
}
Initialization of Dealer requires initialization of Motorcycle m_motorcycles[3];, when it tries to create an array of Motorcycle it looks for its default constructor, but only a parameterized constructor is provided.
class Motorcycle{
public:
string m_make;
string m_model;
int m_year;
Motorcycle(string, string, int);
Motorcycle(){
//.....
};
};
Adding a default constructor to Motorcycle would be one way of fixing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With