2d指针初始化

时间:2013-11-26 21:36:44

标签: c++ dynamic-arrays

我的班级中有这个数据成员:cell ** rep。我想在构造函数中设置2d的大小。编译时我没有收到任何错误;但是当我跑的时候没有结果。

// main.cpp中

    #include <ctime>
    #include <cstdlib>
    #include "cell.h"
    #include "world.h"
    using namespace std;

    int main(){
srand(time(0)); 
World Conway(6,6);
Conway.generateWorld();     
int numAlive = 1;
do{
    numAlive = Conway.print();

    Conway.nextGeneration();    

    cout<<"\n\t\t\t*****************\n\n";
    cin.get();
}while(numAlive);
cin.get();
return 0;
  }

// cell.h

#ifndef CELL_H
#define CELL_H
class Cell {
private :
    bool alive;
public :
    Cell() ;
    void setAlive(bool b);
    bool isAlive();
};
#endif

// cell.cpp

#include "cell.h" // class's header file
Cell::Cell(){
alive = false;
}

void Cell::setAlive(bool b){
alive = b;
}

bool Cell::isAlive(){
return alive;
}

// world.h

#ifndef WORLD_H
#define WORLD_H
#include "Cell.h"
class World {
private :
    bool ring;
    int lines, columns ;
    Cell** rep; //I could not write this with pointer
public :
    World (int l, int c) ;
    World (int l, int c, bool ring);
    ~World() ;
    int getLines();
    int getColumns();
    void generateWorld();
    int nbAliveNeighbor( int i, int j) ;
    int nbAliveNeighborRing( int i, int j);
    void nextGeneration();
    int print();    //the output of this function help me to end main loop

} ;
#endif

// world.cpp

#include "world.h" // class's header file
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

World::World (int l, int c) {
lines = l;
columns = c;
ring = false;
Cell **rep = 0;
// memory allocated for elements of rows. 
rep = new Cell *[l];
// memory allocated for  elements of each column.  
for( int i = 0 ; i < l ; i++ ) {
    rep[i] = new Cell[c];
}

}

World::World(int l,int c,bool r){
lines = l;
columns = c;
ring = r;   
 }

int World::getLines(){
return lines;
}

int World::getColumns(){
return columns;
}

int World::nbAliveNeighborRing( int i, int j){
int n = 0;  //number of alives      
for(int r = i-1; r < i+2; r++){
    for(int c = j-1; c < j+2; c++){

        //giving ring flexibility
        if(c < 0)
            c + columns;
        if(c >= columns)
            c - columns;

        if(r < 0)
            r + lines;
        if(r >= lines)
            r - lines;



        if(c==j && r ==i)
            continue;   //ignoring the cell itself

        if(rep[r][c].isAlive())
            n++;                
    }
}
return n;
}

int World::nbAliveNeighbor( int i, int j) {
int n = 0;  //number of alives      
for(int r = i-1; r < i+2; r++){
    for(int c = j-1; c < j+2; c++){

        //ignoring if it's out of range
        if(c < 0 || c >= columns)
            continue;   
        if(r < 0 || r >= lines)
            continue;

        //ignoring the cell itself
        if(c==j && r ==i)
            continue;

        if(rep[r][c].isAlive())
            n++;                
    }
}
return n;

}
int random(int a,int b){
return a+rand()%(b-a+1);    //including a & b
}

void World::generateWorld(){
int nAlive = (lines * columns)/ 4 + 1;
//why plus 1:
// because in random some are the same so we plus it with 1 so in the average
// the alive cells will be third of deads!


int randAry[nAlive];

for(int i=0, clm=0, row=0; i < nAlive; i++){
    randAry[i] = random(0,lines*columns);
    clm = 0;
    row = 0;
    while(randAry[i] >= lines){
        row ++;
        randAry[i] -= lines;
    }
    clm = randAry[i];
    rep[row][clm].setAlive(true);
}

}

void World::nextGeneration(){
if(ring){
    for(int i = 0; i < lines; i++){
        for(int j = 0; j < columns; j++){
            if(rep[i][j].isAlive()){
                if(nbAliveNeighborRing(i,j) == 3 || nbAliveNeighborRing(i,j) == 2)
                    continue;
                else
                    rep[i][j].setAlive(false);  
            }
            else{
                if(nbAliveNeighborRing(i,j) == 3)
                    rep[i][j].setAlive(true);                   
            }
        }
    }
}
else{
    for(int i = 0; i < lines; i++){
        for(int j = 0; j < columns; j++){
            if(rep[i][j].isAlive()){
                if(nbAliveNeighbor(i,j) == 3 ||        nbAliveNeighbor(i,j) == 2){
                    continue;
                }
                else{
                    rep[i][j].setAlive(false);
                }
            }
            else{
                if(nbAliveNeighbor(i,j) == 3){
                    rep[i][j].setAlive(true);
                }
            }
        }
    }

}
}

int World::print(){
int n = 0;
for(int i = 0; i < lines; i++){
    for(int j = 0; j < columns; j++){
        if(rep[i][j].isAlive()){
            cout<<" * ";
            n++;
        }
        else
            cout<<" - ";
    }
    cout<<endl;
}
return n;
} 

World::~World(){
delete rep;
}

3 个答案:

答案 0 :(得分:1)

您的Cell** rep永远不会被创建。这应该在构造函数中完成。现在你正在制作int **rep = 0;,似乎没有在任何地方使用。

//int **rep = 0; //scratch this
rep = new Cell *[l];
// memory allocated for  elements of each column.  
for( int i = 0 ; i < l ; i++ ) {
    rep[i] = new Cell[c];
}

答案 1 :(得分:0)

也许Cell.h在world.h的包含中大写。

答案 2 :(得分:0)

在类World中,数据成员rep被定义为

class World {
private :
    bool ring;
    int lines, columns ;
    Cell** rep; //I could not write this with pointer
...

但是在构造函数中,您使用类型为int **的局部变量rep,并尝试初始化在退出构造函数后将被销毁的局部变量。

World::World (int l, int c) {
lines = l;
columns = c;
ring = false;
int **rep = 0;
// memory allocated for elements of rows. 
rep = new int *[l];
// memory allocated for  elements of each column.  
for( int i = 0 ; i < l ; i++ ) {
    rep[i] = new int[c];
}
相关问题