#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//In this section I create the class HotelRoom
class HotelRoom
{
protected:
HotelRoom(){}
char room_Number[4];
char* guestName;
double rate;
public:
HotelRoom(char Num[], double daily, char* name);
HotelRoom(const HotelRoom &);
~HotelRoom();
int Get_Capacity();
int Get_Status();
double Get_Rate();
const char* Get_Number();
const char* Get_Guest();
int Change_Status(int Stat);
};
HotelRoom::HotelRoom(char Num[], double daily, char* name)
{
strcpy_s(room_Number, 4, Num);
guestName = new char[strlen(name) + 1];
strcpy_s(guestName, 20, name);
rate = daily;
}
HotelRoom::HotelRoom(const HotelRoom& room_r)
{
strcpy_s(room_Number, room_r.room_Number);
guestName = new char[strlen(room_r.guestName) + 1];
strcpy_s(guestName, 20, room_r.guestName);
rate = room_r.rate;
}
HotelRoom::~HotelRoom()
{
cout << endl << endl;
cout << "Decunstructor Activated." << endl;
delete[] guestName;
}
double HotelRoom::Get_Rate()
{
return rate;
}
const char* HotelRoom::Get_Number()
{
return room_Number;
}
const char* HotelRoom::Get_Guest()
{
return guestName;
}
class DerivedGuestRoom : public HotelRoom
{
public:
DerivedGuestRoom(int max, int stat, int nights);
DerivedGuestRoom(const DerivedGuestRoom&);
~DerivedGuestRoom();
protected:
int capacity;
int status = 0; //0 if unoccupied
int days;
};
DerivedGuestRoom::DerivedGuestRoom(int max, int stat, int nights) : capacity(max), status(stat), days(nights)
{
cout << "data members set";
}
DerivedGuestRoom::~DerivedGuestRoom()
{
cout << "\ndecunstrucor";
}
class DerivedMeetingRoom : public HotelRoom
{
public:
DerivedMeetingRoom(int seat, int stat);
DerivedMeetingRoom(const DerivedMeetingRoom&);
~DerivedMeetingRoom();
protected:
int seats;
int status; //1 if booked for meeting 0 otherwise
};
DerivedMeetingRoom::DerivedMeetingRoom(int seat, int stat) : seats(seat), status(stat)
{
cout << "data members set";
}
DerivedMeetingRoom::~DerivedMeetingRoom()
{
cout << "\ndecunstrucor";
}
void Display_info(HotelRoom&);
HotelRoom* Create_Hotel();
int main()
{
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
HotelRoom* Hotel_Rooms[200];
int count = 0;
char response;
cout << endl;
cout << "Do you want to enter information about a hotel room?(Y/N): ";
response = cin.get();
cin.get();
while (toupper(response) == 'Y' && count < 10)
{
Hotel_Rooms[count] = Create_Hotel();
++count;
cout << endl;
cout << "Do you want to enter information about a hotel room?(Y/N): ";
response = cin.get();
cin.get();
}
//Display the accounts created
for (int i = 0; i < count; ++i)
Display_info(*Hotel_Rooms[i]);
for (int i = 0; i < count; ++i)
delete Hotel_Rooms[i];
cout << endl;
system("PAUSE");
return 0;
}
void Display_info(HotelRoom& room)
{
cout << "\n\nGuest's Name: " << room.Get_Guest() << endl << endl;
cout << "\nYour room number is " << room.Get_Number() << endl << endl;
cout << "\nDaily rate is: " << room.Get_Rate() << endl << endl;
cout << endl;
cout << endl;
}
HotelRoom* Create_Hotel()
{
//These are the variables that will be holding data then passed through objects
char roomNumber[4];
char guestName[20];
double dailyRate = 89.00;
HotelRoom* room_ptr;
//This portion asks for user input
cout << "\nEnter Guest information\n\n";
cout << "Enter the room number: ";
cin.getline(roomNumber, 4);
cout << "Enter the guest's name: ";
cin.getline(guestName, 20);
cout << endl;
cin.get(); //Clears input buffer
room_ptr = new HotelRoom(roomNumber, dailyRate, guestName);
return room_ptr;
}
我拿出了HotelRoom();从父类构造函数和我从子类构造函数中删除。我现在只有这个错误:
LearningOOP.exe已触发断点。
我从未遇到过这个,所以不确定它是什么意思。
答案 0 :(得分:2)
您尚未为HotelRoom类定义默认构造函数。
class HotelRoom
{
protected:
char room_Number[4];
char* guestName;
double rate;
public:
HotelRoom(); //< Not defined!
HotelRoom(char Num[], double daily, char* name);
HotelRoom(const HotelRoom &);
~HotelRoom();
int Get_Capacity();
int Get_Status();
double Get_Rate();
const char* Get_Number();
const char* Get_Guest();
int Change_Status(int Stat);
};
“未解析的外部符号”链接器错误意味着您已在应用程序中声明并使用了一段代码,但在使用之前尚未对其进行定义。
编辑(根据您在下面的跟进评论):
您无法将构造函数取出,因为代码的其他部分需要它。请在代码中查看此行:
DerivedGuestRoom::DerivedGuestRoom(int max, int stat, int nights) : HotelRoom(), capacity(max), status(stat), days(nights)
// ^---------^
// |
// Using the default constructor.
DerivedMeetingRoom::DerivedMeetingRoom(int seat, int stat) : HotelRoom(), seats(seat), status(stat)
// ^---------^
// |
// And again here!
您需要删除实现HotelRoom
类的默认构造函数,或者向DerivedMeetingRoom
和DerivedGuestRoom
类构造函数添加参数,以便您可以使用HotelRoom(char Num[], double daily, char* name)
构造
答案 1 :(得分:2)
您声明了默认构造函数:
class HotelRoom
{
public:
HotelRoom();
}
并且没有该默认构造函数方法的实现。您可以更改为:
class HotelRoom
{
public:
HotelRoom() {}
}
或在cpp文件中实现HotelRoom:HotelRoom { }
。