没有默认构造函数?
No default constructor?
#include <iostream>
using namespace std;
//To know the car's make, model, color, license number, and the number of minutes the car has been parked
class ParkedCar
{
string make, model, color, license_number;
int minutes;
public:
ParkedCar(string make = "...", string model = "...", string color = "...",
string license_number = "...", int minutes = 0) : make{ make }, model{model},
color{color}, license_number{license_number}, minutes{minutes} { this->make = make; }
};
//To know the number of minutes of parking time that has been purchased
class ParkingMeter
{
int purchased_minutes;
public:
ParkingMeter(int purchased_minutes) { this->purchased_minutes = purchased_minutes; }
};
//To report the make, model, color, and license number of the illegally parked car
//To report the amount of the fine, which is for the first hour, or part of an
//hour that the car is illegally parked, plus for every additional hour or
//part of an hour that the car is illegally parked
//To report the name and badge number of the police officer issuing the ticket
class ParkingTicket
{
ParkedCar car;
ParkingMeter meter;
PoliceOfficer officer;
public:
ParkingTicket(const ParkedCar &car, const ParkingMeter &meter, const PoliceOfficer &officer)
{
this->car = car;
this->meter = meter;
this->officer = officer;
}
};
//To know the officer's name and badge number
//To examine a ParkedCar object and a ParkingMeter object, and determine whether the car's time has expired
//To issue a prking ticket (generate a ParkingTicket object) if the car's time has expired
class PoliceOfficer
{
string name;
int badge_number;
ParkedCar car;
ParkingMeter meter;
public:
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
{
this->car = car;
this->meter = meter;
}
};
所以我正在为学校制作这个文件,在我到达停车票和警察 classes 之前一切都很好。我正在尝试定义这些构造函数,它们都告诉我没有另一个 class 的默认构造函数。我将如何解决这个问题?谢谢! :)
因为你的构造函数
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
{
this->car = car;
this->meter = meter;
}
没有 car
或 meter
的成员初始化器(正文中的赋值不算作初始化器),就好像你写了 : car(), meter()
初始化器,它尝试调用不存在的默认构造函数。您应该显式编写成员初始值设定项:
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
: car(car), meter(meter)
{
}
(还有,你的对象模型有点搞笑:警察只有一个停车计时器?)
首先,ParkedCar
(如您所示)有一个默认构造函数(所有参数都有默认值,因此可以不带参数调用它。
真正的解决方案是将构造函数更改为使用初始化列表:
PoliceOfficer(const ParkedCar &car_, const ParkingMeter &meter_, string name = "...", int badge_number = 000000)
: car(car_), meter(meter_)
{
}
#include <iostream>
using namespace std;
//To know the car's make, model, color, license number, and the number of minutes the car has been parked
class ParkedCar
{
string make, model, color, license_number;
int minutes;
public:
ParkedCar(string make = "...", string model = "...", string color = "...",
string license_number = "...", int minutes = 0) : make{ make }, model{model},
color{color}, license_number{license_number}, minutes{minutes} { this->make = make; }
};
//To know the number of minutes of parking time that has been purchased
class ParkingMeter
{
int purchased_minutes;
public:
ParkingMeter(int purchased_minutes) { this->purchased_minutes = purchased_minutes; }
};
//To report the make, model, color, and license number of the illegally parked car
//To report the amount of the fine, which is for the first hour, or part of an
//hour that the car is illegally parked, plus for every additional hour or
//part of an hour that the car is illegally parked
//To report the name and badge number of the police officer issuing the ticket
class ParkingTicket
{
ParkedCar car;
ParkingMeter meter;
PoliceOfficer officer;
public:
ParkingTicket(const ParkedCar &car, const ParkingMeter &meter, const PoliceOfficer &officer)
{
this->car = car;
this->meter = meter;
this->officer = officer;
}
};
//To know the officer's name and badge number
//To examine a ParkedCar object and a ParkingMeter object, and determine whether the car's time has expired
//To issue a prking ticket (generate a ParkingTicket object) if the car's time has expired
class PoliceOfficer
{
string name;
int badge_number;
ParkedCar car;
ParkingMeter meter;
public:
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
{
this->car = car;
this->meter = meter;
}
};
所以我正在为学校制作这个文件,在我到达停车票和警察 classes 之前一切都很好。我正在尝试定义这些构造函数,它们都告诉我没有另一个 class 的默认构造函数。我将如何解决这个问题?谢谢! :)
因为你的构造函数
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
{
this->car = car;
this->meter = meter;
}
没有 car
或 meter
的成员初始化器(正文中的赋值不算作初始化器),就好像你写了 : car(), meter()
初始化器,它尝试调用不存在的默认构造函数。您应该显式编写成员初始值设定项:
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
: car(car), meter(meter)
{
}
(还有,你的对象模型有点搞笑:警察只有一个停车计时器?)
首先,ParkedCar
(如您所示)有一个默认构造函数(所有参数都有默认值,因此可以不带参数调用它。
真正的解决方案是将构造函数更改为使用初始化列表:
PoliceOfficer(const ParkedCar &car_, const ParkingMeter &meter_, string name = "...", int badge_number = 000000)
: car(car_), meter(meter_)
{
}