我在编写有关机场服务的代码时遇到问题,其中包含两个类、构造函数、运算符等。我不允许更改主要功能,那是我被分配要做的事情。 我被“没有匹配的‘AirportService’初始化构造函数”问题难住了,除非我更改主要功能,否则我似乎无法解决这个问题。我能对如何解决这个问题有任何见解吗? 这是我正在使用的代码
#include <iostream>
#include <cstring>
using namespace std;
const int FAULTY = -1;
const int IN_REPAIR = 1;
const int FIXED=0;
class Airplane {
private:
char* reg_mark;
char* airline_name;
char* home_airport;
int flight_hours;
int status;
public:
Airplane() {
reg_mark = new char[1];
reg_mark[0] = '\0';
airline_name = new char[1];
airline_name[0] = '\0';
home_airport = new char[1];
home_airport[0] = '\0';
flight_hours = 0;
status = 0;
}
Airplane(const char* rm, const char* an, const char* ha, int fh, int s = 0) {
reg_mark = new char[strlen(rm) + 1];
strcpy(reg_mark, rm);
airline_name = new char[strlen(an) + 1];
strcpy(airline_name, an);
home_airport = new char[strlen(ha) + 1];
strcpy(home_airport, ha);
flight_hours = fh;
status = s;
}
Airplane(const Airplane& p) {
reg_mark = new char[strlen(p.reg_mark) + 1];
strcpy(reg_mark, p.reg_mark);
airline_name = new char[strlen(p.airline_name) + 1];
strcpy(airline_name, p.airline_name);
home_airport = new char[strlen(p.home_airport) + 1];
strcpy(home_airport, p.home_airport);
flight_hours = p.flight_hours;
status = p.status;
}
~Airplane() {
delete[] reg_mark;
delete[] airline_name;
delete[] home_airport;
}
Airplane& operator=(const Airplane& p) {
if (this != &p) {
delete[] reg_mark;
delete[] airline_name;
delete[] home_airport;
reg_mark = new char[strlen(p.reg_mark) + 1];
strcpy(reg_mark, p.reg_mark);
airline_name = new char[strlen(p.airline_name) + 1];
strcpy(airline_name, p.airline_name);
home_airport = new char[strlen(p.home_airport) + 1];
strcpy(home_airport, p.home_airport);
flight_hours = p.flight_hours;
status = p.status;
}
return *this;
}
void print() const {
cout << reg_mark << " " << airline_name << " " << home_airport << " " << flight_hours << endl;
}
const char* getRegMark() const {
return reg_mark;
}
const char* getAirlineName() const {
return airline_name;
}
const char* getHomeAirport() const {
return home_airport;
}
int getFlightHours() const {
return flight_hours;
}
int getStatus() const {
return status;
}
void setStatus(int s) {
status = s;
}
bool operator==(const Airplane& p) const {
return strcmp(airline_name, p.airline_name) == 0;
}
bool operator>(const Airplane& p) const {
return flight_hours > p.flight_hours || (flight_hours == p.flight_hours && strcmp(home_airport, p.home_airport) != 0);
}
};
class AirportService {
private:
string airport_name;
Airplane* airplanes;
int num_airplanes;
public:
AirportService(string name) : airport_name(name), airplanes(nullptr), num_airplanes(0) {}
~AirportService() {
delete[] airplanes;
}
void addAirplane(Airplane plane) {
if (plane.getStatus() == FAULTY) {
bool found_same_airline = false;
for (int i = 0; i < num_airplanes; i++) {
if (strcmp(airplanes[i].getAirlineName(), plane.getAirlineName()) == 0) {
found_same_airline = true;
if (airplanes[i].getFlightHours() > plane.getFlightHours()) {
return; // don't add the new plane
} else {
airplanes[i].setStatus(IN_REPAIR);
}
}
}
if (!found_same_airline) {
Airplane* new_airplanes = new Airplane[num_airplanes + 1];
for (int i = 0; i <= num_airplanes; i++) {
new_airplanes[i] = airplanes[i];
}
new_airplanes[num_airplanes] = plane;
new_airplanes[num_airplanes].setStatus(IN_REPAIR);
delete[] airplanes;
airplanes = new_airplanes;
num_airplanes++;
} else {
delete[] airplanes;
}
}
}
Airplane serviceOldestAirplane() {
int oldest_index = 0;
for (int i = 1; i < num_airplanes; i++) {
if (airplanes[i].getFlightHours() > airplanes[oldest_index].getFlightHours()) {
oldest_index = i;
} else if (airplanes[i].getFlightHours() == airplanes[oldest_index].getFlightHours()) {
if (strcmp(airplanes[i].getHomeAirport(), airport_name.c_str()) == 0) {
continue;
} else if (strcmp(airplanes[oldest_index].getHomeAirport(), airport_name.c_str()) == 0) {
oldest_index = i;
}
}
}
Airplane oldest_plane = airplanes[oldest_index];
Airplane* new_airplanes = new Airplane[num_airplanes - 1];
int j = 0;
for (int i = 0; i < num_airplanes; i++) {
if (i != oldest_index) {
new_airplanes[j] = airplanes[i];
j++;
}
}
delete[] airplanes;
airplanes = new_airplanes;
num_airplanes--;
oldest_plane.setStatus(FIXED);
return oldest_plane;
}
void print() {
for (int i = 0; i < num_airplanes; i++) {
if (airplanes[i].getStatus() == IN_REPAIR) {
airplanes[i].print();
}
}
}
};
int main() {
int testCase;
cin >> testCase;
char ID[12];
char company_name[20];
char company_airport[20];
int flight_hours;
int state;
if (testCase == 1) {
cout << "TESTING CONSTRUCTOR FOR AIRPLANE" << endl;
Airplane a;
cout << "TEST FOR DEFAULT CONSTRUCTOR PASSED" << endl;
Airplane a1("ZHN-96-TY", "FINKI-Airline", "TMF", 13);
cout << "TEST FOR CONSTRUCTOR WITH 3 ARGUMENTS PASSED" << endl;
Airplane a2("ZHN-96-TA", "FINKI1-Airline", "TMF", 13, 0);
cout << "TEST FOR CONSTRUCTOR WITH 4 ARGUMENTS PASSED" << endl;
} else if (testCase == 2) {
cout << "TESTING COPY-CONSTRUCTOR AND OPERATOR = (ASSIGNMENT) FOR AIRPLANE" << endl;
Airplane p("ZHN-96-TA", "FINKI-Airline", "TMF", 13, 0);
Airplane p1(p);
cout << "TEST FOR COPY CONSTRUCTOR PASSED" << endl;
Airplane p2;
p2 = p;
cout << "TEST FOR OPERATOR = (ASSIGNMENT) PASSED" << endl;
} else if (testCase == 3) {
cout << "TESTING PRINT() FOR AIRPLANE" << endl;
cin>>ID>>company_name>>company_airport>>flight_hours>>state;
Airplane p(ID, company_name, company_airport, flight_hours, state);
p.print();
} else if (testCase == 4) {
cout << "TESTING CONSTRUCTOR FOR AIRPORTSERVICE" << endl;
AirportService as("FINKI");
cout << "TEST PASSED" << endl;
} else if (testCase == 5) {
cout << "TESTING ADD() AND PRINT() FOR AIRPORTSERVICE" << endl;
AirportService as("FINKI");
int n;
cin>>n;
for(int i=0; i<n; i++){
cin>>ID>>company_name>>company_airport>>flight_hours>>state;
Airplane p(ID, company_name, company_airport, flight_hours, state);
as.addAirplane(p);
}
as.print();
} else if (testCase == 6) {
cout << "TESTING serviceOldestAirplane() AND PRINT() FOR AIRPORTSERVICE" << endl;
AirportService as("FINKI");
int n;
cin>>n;
for(int i=0; i<n; i++){
cin>>ID>>company_name>>company_airport>>flight_hours>>state;
Airplane p(ID, company_name, company_airport, flight_hours, state);
as.addAirplane(p);
}
Airplane p = as.serviceOldestAirplane();
cout<<"Removed plane:"<<endl;
p.print();
cout<<"-----------------"<<endl;
as.print();
} else if (testCase == 7) {
cout << "TESTING COPY CONSTRUCTOR AND OPERATOR = FOR AIRPORTSERVICE" << endl;
AirportService as("FINKI");
Airplane a1("ZHN-96-TY", "FINKI-Airline", "TMF", 13);
as.addAirplane(a1);
AirportService s1 = as; //copy constructor
AirportService s2;
s2 = s1; //operator =
s1.print();
s2.print();
}
return 0;
}
我试图修复复制构造函数但无济于事,也许我只是做错了。就我而言,唯一不允许的修复方法是将“as”值分配给 AirportService s2。 在不更改主要功能代码的情况下,是否可以解决此问题?
类
AirportService
没有这个声明中使用的默认构造函数
AirportService s2;
要么声明构造函数,要么使用类的现有构造函数的参数重写声明。