我试图输入房间,按价格进行排序,然后创建一个文本文件,但总是有一个错误,我怀疑这与功能的大小有关。
在一些编译器中,它会编译,但是它会有警告消息:
警告:对数组函数参数“房间”的“大小”将返回“房间类型*”-Wsizeof数组-参数的大小
Dev C++根本不会编译
#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;
}
}
发布于 2020-08-01 12:30:54
对于指针和数组,您有常见的新手混淆。以这段代码为例
void sort(roomType rooms[roomamt]) {
int n = sizeof(rooms)/sizeof(rooms[0]);
sort(rooms, rooms+n, comparePrice);
}
在此代码中,rooms
是一个指针。它可能看起来像一个数组,但它不是。在C++中不可能使数组成为一个函数参数。因此编译器将roomType rooms[roomamt]
更改为roomType* rooms
。因此,sizeof(rooms)
是指针的大小,而不是数组的大小,因此对数组大小的计算是不正确的。
但是在您的例子中,修复很容易,只需使用roomant
void sort(roomType* rooms) {
sort(rooms, rooms + roomamt, comparePrice);
}
注意,我已经更改了代码,将rooms
显示为它真正的指针。
然而,即使没有这个修复,我也不明白为什么您的代码会崩溃(或者无法编译)。
发布于 2020-08-01 12:42:30
您可以使用C++数组而不是C数组。C++数组可以通过值和引用函数传递。它们可以从函数中返回,并且可以简单地复制。它们包含它们的大小。所有这些优点都有0的开销。
#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;
}
}
发布于 2020-08-01 12:39:17
数组在传递给函数时会衰减为指针。这个签名
void sort(roomType rooms[roomamt]){
等于
void sort(roomType* rooms){
这有点不幸,因为它助长了对数组和指针的误解。当您调用该函数时
sort(rooms);
然后,数组rooms
被隐含地转换为指向数组中的第一个元素的指针。重要的是,在主rooms
中是一个数组,但是在您的函数中,rooms
是一个指针。它们是不同的类型,大小信息丢失。
可以通过引用传递数组来规避这一问题(详细信息请参见here )。使用std::array
(编译时已知的大小)或std::vector
(动态大小)要容易得多。
https://stackoverflow.com/questions/63205172
复制相似问题