C++ c+内部无效+;结构不';不能更改一个结构值
我有这个结构C++ c+内部无效+;结构不';不能更改一个结构值,c++,function,struct,pass-by-reference,C++,Function,Struct,Pass By Reference,我有这个结构 typedef struct _flight { FlightId flightId; int totalPlaces; int booked; float currentPrice; float initialPrice; void Print(int index); void RecalculatePrice(); bool BookSeats(int amount); } Flight; 这就造成了麻烦
typedef struct _flight {
FlightId flightId;
int totalPlaces;
int booked;
float currentPrice;
float initialPrice;
void Print(int index);
void RecalculatePrice();
bool BookSeats(int amount);
} Flight;
这就造成了麻烦,比如:
bool Flight::BookSeats(int cantidad) {
if (booked + cantidad <= totalPlaces) {
booked = booked + cantidad;
RecalculatePrice();
return true;
}
return false;
}
我做错了什么?我不知道这是否是一个参考问题,但是。。。变量和void都在结构内部。我不明白。在使用变量之前,您需要通过设置一个值来初始化它。 如果编译器接受c++11,则可以直接在类/结构中执行,如下所示:
struct Flight {
int booked = 0; // in-class initialization (c++11)
};
对于旧的C++编译器,飞行构造函数应该初始化其成员:
struct Flight {
Flight() : booked(0) {}
int booked;
};
确保条件满足?如果你能给出一个完整的例子会更好吗?
Flight
的构造器是什么样子的?我们为什么需要MCVE的教科书示例。如果你还没有编写一个,那么你还没有完成你的调试。如果你没有“<代码> sieOS/<代码>”,那么有些事情严重错误,你实际上没有编写C++,这是毫无意义的。你可以在评论中读到,我不“构建”结构,我用C风格声明和设置为零。code>flightf={0}@DrkDeveloper:不要在注释中隐藏该信息,而是生成一个完整的问题示例。这样,像alex这样的人就不会浪费时间随机猜测代码的功能了。
struct Flight {
Flight() : booked(0) {}
int booked;
};