c++ - 构造函数不工作

标签 c++ constructor

<分区>

我正在用 C++ 编写一个程序来展示康威的生命游戏。我的教授给了我们一个主要功能和一个描述“宇宙”的类,我们必须实现类中原型(prototype)化的功能。我现在的问题实际上是让构造函数发挥作用。我将发布类(class),然后是我为构造函数编写的内容。当我到达使用构造函数的第一行时使用 GDB (universe(width, height, wrap);) 我收到以下错误:libc++abi.dylib: terminate called throwing an exception

程序收到信号 SIGABRT,已中止。 __kill () 中的 0x00007fff876fad46

感谢任何帮助!下面的代码。

    // Conways game of life

    class universe {             
private:
int* array;     // A pointer to a [width]X[height] array of cells that constitutes       the universe
                //    for the game of life. A value of 1 signifies a live cell at that location.
int width;      // The width of the universe.
int height;     // The height of the universe
int wrap;       // wrap = 1 means the universe wraps around on itself -- the right hand 
                //   side connects to the left and the top connects to the bottom.
                //   wrap = 0 means the universe ends at the array boundaries.
public:
universe();         // A null constructor: sets array, width, height, and wrap to 0.
universe(int,int,int);  // Constructor to allocate space for a universe of given width (first value) 
                    //    height (second value) and wrap setting (third value). Sets all cells to 0.
void display();     // Display the live cells in a graphics window.
void setup();       // Display the universe then allow the user to interactively modify 
                    //    the cell arrangement using the mouse.
void operator<<(char*); // Read in a universe from a file; first number has the width,
                            //    second number is the height,
                            //    third number is the wrap parameter,
                            //    then 1s/0s in a 2D integer array represent living/dead cells.   
void operator>>(char*); // Save the universe to a file with the specified name (format as above).
void operator=(universe);   // Copy the contents of one universe to another.
void operator<<(universe);  // Calculate the new generation by applying the rules, then
                            //    display the new generation.
int neighbors(int,int);     // Returns the number of neighbors of the cell at i,j.
int value(int,int);        // Returns the value at cell i,j (0 or 1).
void setvalue(int,int,int); // Sets the value of the cell at i,j.  
void free(); // Release the memory used to store the universe. Set array = 0. 
};

// Implementation


universe::universe(){
array =0;               
width = 0;
height = 0;
wrap = 0;
}

universe::universe(int width1,int height1,int wrap1){

int i=0, j=0;
int* array = new int[width*height-1];       
for(i=0;i<width;i++){
    for(j=0;j<height;j++){
        array[j*width+i] =0;
                        }
                    }
width = width1;
height =height1;
wrap = wrap1;
}

最佳答案

int* array = new int[width*height-1];  

应该是

array = new int[width*height-1];  

因为 array 是您的类成员,所以您不应该再次声明同名的局部变量。否则,您没有初始化类成员 array。局部变量将隐藏类成员。

与此同时,您应该先为 widthheight 赋值,然后再将它们用于 new

您的代码应如下所示:

 universe::universe(int width1,int height1,int wrap1){
    width = width1;
    height =height1;
    wrap = wrap1;
    array = new int[width*height-1];       
    for(int i=0; i<width; i++){
      for(int j=0; j<height; j++){
         array[j*width+i] =0;
      }
    }
}

如果您想初始化列表,最好将成员 int*array 放在 wrap 之后。

关于c++ - 构造函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073379/

相关文章:

c++ - 如何使用基于数组的二进制堆访问最小元素?

c++ - 关于 vector 的困惑

c# - 实例化:将列表移动到参数中

c++ - Arduino 中不需要的符号扩展

c++ - 如何在没有拷贝的情况下使右值引用传递的对象保持事件状态?

c++ - COM 初始化和在 Win32 C++ DLL 中的使用

c++ - 构造函数是否创建类的对象?

methods - F# - 调用方法并分配给构造函数中的属性

c++ - C++ 什么时候调用作为类成员的对象的构造函数?

c++ - vector 是链表的特例吗?