我试图输入房间,按价格排序,然后创建一个文本文件,但总是有一个错误,我怀疑与sizeof函数有关
在某些编译器中,它将进行编译,但会出现警告消息:
警告:数组函数参数“rooms”上的“sizeof”将返回“roomtype*”[-wsizeof-array-argument]的大小
DevC++根本不会编译
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
const int roomamt = 4;
struct package
{
int numRoom;
int numNight;
};
struct roomType
{
string type;
string breakfast;
int price;
package packageType;
};
bool comparePrice(roomType p1, roomType p2)
{
return (p1.price < p2.price);
}
void input(roomType rooms[roomamt]);
void sort(roomType rooms[roomamt]);
int main()
{
roomType rooms[roomamt];
input(rooms);
sort(rooms);
}
void input(roomType rooms[roomamt]){
for(int i = 0; i < roomamt; i++){
cout << "Enter type of room: ";
cin >> rooms[i].type;
cout << "Specify if breakfast included: ";
cin >> rooms[i].breakfast;
cout << "Enter price of room/night: ";
cin >> rooms[i].price;
cout << "Enter num of room: ";
cin >> rooms[i].packageType.numRoom;
cout << "Enter num of nights: ";
cin >> rooms[i].packageType.numNight;
}
}
void sort(roomType rooms[roomamt]){
int n = sizeof(rooms)/sizeof(rooms[0]);
sort(rooms, rooms+n, comparePrice);
}
void createFile(roomType rooms[roomamt]){
ofstream priceList("priceList.txt");
priceList << setw(20) << "Room Type" << setw(20) << "Breakfast" << setw(20) << "Price/night" << setw(20) << "Room" << setw(20) << "Nights" << endl;
priceList << setw(20) << "---------" << setw(20) << "----" << setw(20) << "-----" << setw(20) << "----" << setw(20) << "----" << endl;
for(int i = 0; i < roomamt; i++){
priceList << setw(20) << rooms[i].type << setw(20) << rooms[i].breakfast << setw(20) << rooms[i].price << setw(20) << setw(20) << rooms[i].packageType.numRoom << setw(20) << rooms[i].packageType.numNight << endl;
}
}
对于指针和数组,您有常见的新手混淆。 以以下代码为例
void sort(roomType rooms[roomamt]) {
int n = sizeof(rooms)/sizeof(rooms[0]);
sort(rooms, rooms+n, comparePrice);
}
在此代码中,rooms
是一个指针。 它可能看起来像一个数组,但实际上不是。 在C++中,使数组成为函数参数是不可能的。 因此编译器将RoomTypeRooms[roomamt]
更改为RoomType*Rooms
。 因此,sizeof(rooms)
是指针的大小,而不是数组的大小,因此您对数组大小的计算是不正确的。
但在您的情况下,修复很容易,只需使用roomant
void sort(roomType* rooms) {
sort(rooms, rooms + roomamt, comparePrice);
}
注意,这里我更改了代码,将rooms
显示为真正的指针。
然而,即使没有这个修复,我也不明白为什么您的代码会崩溃(或者编译失败)。
您可以使用C++数组而不是C数组。 C++数组可以通过值传递,也可以通过对函数的引用传递。 它们可以从函数中返回,也可以简单地复制。 它们包含它们的大小。 所有这些优点都是零开销的。
#include <array>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
constexpr int roomamt = 4;
struct package
{
int numRoom;
int numNight;
};
struct roomType
{
string type;
string breakfast;
int price;
package packageType;
};
bool comparePrice(roomType p1, roomType p2)
{
return (p1.price < p2.price);
}
void input(array<roomType, roomamt> &rooms);
void sort(array<roomType, roomamt> &rooms);
int main()
{
array<roomType, roomamt> rooms;
input(rooms);
sort(rooms);
}
void input(array<roomType, roomamt> &rooms){
for(int i = 0; i < rooms.size(); i++){
cout << "Enter type of room: ";
cin >> rooms[i].type;
cout << "Specify if breakfast included: ";
cin >> rooms[i].breakfast;
cout << "Enter price of room/night: ";
cin >> rooms[i].price;
cout << "Enter num of room: ";
cin >> rooms[i].packageType.numRoom;
cout << "Enter num of nights: ";
cin >> rooms[i].packageType.numNight;
}
}
void sort(array<roomType, roomamt> &rooms){
sort(rooms.begin(), rooms.end(), comparePrice);
}
void createFile(array<roomType, roomamt> &rooms){
ofstream priceList("priceList.txt");
priceList << setw(20) << "Room Type" << setw(20) << "Breakfast" << setw(20) << "Price/night" << setw(20) << "Room" << setw(20) << "Nights" << endl;
priceList << setw(20) << "---------" << setw(20) << "----" << setw(20) << "-----" << setw(20) << "----" << setw(20) << "----" << endl;
for(int i = 0; i < rooms.size(); i++){
priceList << setw(20) << rooms[i].type << setw(20) << rooms[i].breakfast << setw(20) << rooms[i].price << setw(20) << setw(20) << rooms[i].packageType.numRoom << setw(20) << rooms[i].packageType.numNight << endl;
}
}
数组传递给函数时衰减为指针。 此签名
void sort(roomType rooms[roomamt]){
相当于
void sort(roomType* rooms){
这有点不幸,因为它助长了关于数组与指针的误解。 当您调用函数时
sort(rooms);
然后数组roouss
将被隐含地转换为指向数组中第一个元素的指针。 重要的是,在main中rooms
是一个数组,而在函数中rooms
是一个指针。 它们是不同的类型,大小信息丢失。
这可以通过引用传递数组来规避(详见此处)。 使用std::array
(编译时已知的大小)或std::vector
(动态大小)要容易得多。