English 中文(简体)
C++ 1. 构筑商阵列
原标题:C++ Initialize array in constructor EXC_BAD_ACCESS

I m 创建简单构造和启动一个阵列:

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

保留职能:

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

既定职能:

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?

这是电影档案:

#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
最佳回答

如果设想有18个行文和12个栏目,那就确切地说,你需要把你的阵列包括在内:

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 {
            ...
        }
      }
    }

};
问题回答

页: 1 在某些地方,还是静态? 而且,各层面是否正确? 了解其定义很重要。 否则,可能是原因。 如果这不能解决你的问题,就设定了一个突破点,然后一步步走,看它失败了。

是否真正为保留分配空间?

如果固定尺寸,你是否宣布为<代码>int[18][12]?

如果没有,请不使用<代码>int**。 这是第2版。

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

EDIT:你的问题:

int reservations[17][11];

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

页: 1 您应首先提出行文/栏号(如18和12)而不是最高指数。 当你提出保留时,它将脱离阵列和腐败的<条码>,然后在你试图查阅时,会发生任何事情。

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).

您宣布了一系列的<条码>保留[17][11];,但贵方的构造正在查阅<条码>[0至17][0至11],该代码超出了有效范围<条码>[0至16][0至10]。

您应选择<代码>std:vector<std:vector<int>>。 超过这一阵列。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签