C++在构造函数 EXC_BAD_ACCESS 中初始化数组
我正在创建一个简单的构造函数并初始化一个数组:
// Construtor
Cinema::Cinema(){
// Initalize reservations
for(int i = 0; i < 18; i++){
for(int j = 0; j < 12; j++){
setReservation(i, j, 0);
}
}
// Set default name
setMovieName("N/A");
// Set default price
setPrice(8);
}
setReservation 函数:
void Cinema::setReservation(int row, int column, int reservation){
this->reservations[row][column] = reservation;
}
setMovieName 函数:
void Cinema::setMovieName(std::string movieName){
this->movieName = movieName;
}
由于某些奇怪的原因,当我运行程序时,setMovieName 函数给出以下错误:“程序收到信号:EXC_BAD_ACCESS”
如果我取出for 循环初始化预订数组,问题消失,电影名称设置没有任何问题。知道我做错了什么吗?
这是 Cinema.h 文件:
#ifndef Cinema_h
#define Cinema_h
class Cinema{
private:
int reservations[17][11];
std::string movieName;
float price;
public:
// Construtor
Cinema();
// getters/setters
int getReservation(int row, int column);
int getNumReservations();
std::string getMovieName();
float getPrice();
void setReservation(int row, int column, int reservation);
void setMovieName(std::string movieName);
void setPrice(float price);
};
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果应该有 18 行和 12 列,这正是您需要指定数组尺寸的方式:
最好对这些使用静态常量,而不是“幻数”。此外,行和列很容易混淆,因此更好的想法是为迭代变量
i
和j
赋予更具描述性的名称。If there's supposed to be 18 rows and 12 columns, that's exactly how you need to dimension your array:
Also better use static constants for those, instead of "magic numbers". In addition, rows and columns are easy to confuse, so a better idea is to give more descriptive names to iterating variables
i
andj
.您是否在某处初始化了
this->reservations
还是它是静态的?另外,尺寸是否正确?了解它的定义很重要。否则可能就是这个原因。如果这不能解决您的问题,请设置一个断点,然后逐步运行代码,以查看失败的行。Did you init
this->reservations
somewhere or is it static? Also, are the dimensions correct? Would be important to see its definition. Otherwise that might be the reason. If that doesn't solve your issue, set a break point and then walk the code step by step, to see the line where it fails.您实际上是否分配了预订空间?
如果它是固定大小,您是否将其声明为
int[18][12]
?如果不是,请不要使用
int**
。这是 C++,您可以使用编辑:这就是您的问题:
它有 17/11 维度,您正在迭代 18/12。
使用int[18][12]。
Are you actually allocating space for reservations?
If it's fixed size, are you declaring it as
int[18][12]
?If not, please don't use
int**
. This is c++, you can useEDIT: There's your problem:
This has 17/11 dimensions, you're iterating 18/12.
Use
int[18][12]
.您的
reservations
数组太小。您应该使用行/列数(即 18 和 12)而不是最高索引来初始化它。当您初始化保留时,它将超出数组末尾并损坏movieName
,之后当您尝试访问它时可能会发生任何情况。另外,您可能已经知道这一点,但在 C++ 中,您不需要总是在成员变量访问前面加上
this->
前缀。除非您有一个具有相同名称的局部变量(如 setMovieName 函数中所示),否则这是隐含的。Your
reservations
array is too small. You should initialise it with the number of rows/columns (ie. 18 and 12), not the highest index. When you initialise the reservations it will run off the end of the array and corruptmovieName
, after which anything could happen when you try to access it.Also, you may know this already, but you don't need to always prefix member variable access with
this->
in C++. That's implied unless you have a local variable with the same name (as in your setMovieName function).您声明了一个数组
intservations[17][11];
,但您的构造函数正在访问[0 to 17][0 to 11]
,这超出了有效范围[0 到 16][0 到 10]
。您应该更喜欢
std::vector>
而不是该数组。You declared an array
int reservations[17][11];
, but your constructor is accessing[0 to 17][0 to 11]
, which is beyond the valid ranges[0 to 16][0 to 10]
.You should prefer a
std::vector<std::vector<int>>
over that array.