C++在构造函数 EXC_BAD_ACCESS 中初始化数组

发布于 2024-12-17 10:04:17 字数 1316 浏览 0 评论 0 原文

我正在创建一个简单的构造函数并初始化一个数组:

// 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

I'm creating a simple constructor and initializing an array:

// 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);
}

The setReservation function:

void Cinema::setReservation(int row, int column, int reservation){
    this->reservations[row][column] = reservation;
}

The setMovieName function:

void Cinema::setMovieName(std::string movieName){
    this->movieName = movieName;
}

For some odd reason when I run the program, the setMovieName function gives the following error: "Program Received Signal: EXC_BAD_ACCESS"

If I take out the for-loop that initializes the array of reservations, the problem goes away and the movie name is set without any problems. Any idea what I'm doing wrong?

This is the Cinema.h file:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小情绪 2024-12-24 10:04:17

如果应该有 18 行和 12 列,这正是您需要指定数组尺寸的方式:

int reservations[18][12];

最好对这些使用静态常量,而不是“幻数”。此外,行和列很容易混淆,因此更好的想法是为迭代变量 ij 赋予更具描述性的名称。

class Cinema
{
    static const int row_count = 18;
    static const int column_count = 12;

    int reservations[row_count][column_count];

    //looping
    Cinema() {
      for (int row = 0; row < row_count; ++row) {
        for (int column = 0; column < column_count; ++column {
            ...
        }
      }
    }

};

If there's supposed to be 18 rows and 12 columns, that's exactly how you need to dimension your array:

int reservations[18][12];

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 and j.

class Cinema
{
    static const int row_count = 18;
    static const int column_count = 12;

    int reservations[row_count][column_count];

    //looping
    Cinema() {
      for (int row = 0; row < row_count; ++row) {
        for (int column = 0; column < column_count; ++column {
            ...
        }
      }
    }

};
时间海 2024-12-24 10:04:17

您是否在某处初始化了 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.

旧故 2024-12-24 10:04:17

您实际上是否分配了预订空间?

如果它是固定大小,您是否将其声明为 int[18][12]

如果不是,请不要使用 int**。这是 C++,您可以使用

std::vector<std::vector<int>>

编辑:这就是您的问题:

int reservations[17][11];

它有 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 use

std::vector<std::vector<int>>

EDIT: There's your problem:

int reservations[17][11];

This has 17/11 dimensions, you're iterating 18/12.
Use int[18][12].

蓝天 2024-12-24 10:04:17

您的 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 corrupt movieName, 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).

旧情别恋 2024-12-24 10:04:17

您声明了一个数组 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文