C++ 在中初始化2d指针
我的类中有这个数据成员:cell**rep。我想在构造函数中设置2d的大小。我在编译时没有收到错误;但当我运行它时,没有结果 //main.cppC++ 在中初始化2d指针,c++,dynamic-arrays,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
#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.cpp
#include "cell.h" // class's header file
Cell::Cell(){
alive = false;
}
void Cell::setAlive(bool b){
alive = b;
}
bool Cell::isAlive(){
return alive;
}
//世界卫生组织
#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;
}
#包括“world.h”//class的头文件
#包括
#包括
#包括
使用名称空间std;
世界::世界(整数l,整数c){
直线=l;
列=c;
环=假;
单元格**rep=0;
//为行元素分配的内存。
rep=新单元格*[l];
//为每列的元素分配的内存。
对于(int i=0;i=列)
c柱;
if(r<0)
r+线;
如果(r>=行)
r-线;
如果(c==j&&r==i)
continue;//忽略单元格本身
if(rep[r][c].isAlive())
n++;
}
}
返回n;
}
int World::nbAliveNeighbor(int i,int j){
int n=0;//有效值的数目
对于(int r=i-1;r=列)
继续;
如果(r<0 | | r>=行)
继续;
//忽略单元格本身
如果(c==j&&r==i)
继续;
if(rep[r][c].isAlive())
n++;
}
}
返回n;
}
int随机(int a,int b){
返回a+rand()%(b-a+1);//包括a&b
}
void World::generateWorld(){
int nAlive=(行*列)/4+1;
//为什么加1:
//因为在随机中,有些是相同的,所以我们加上它,平均值是1
//活细胞将是死亡细胞的三分之一!
内兰德尔[纳利夫];
for(inti=0,clm=0,row=0;i=行){
行++;
randAry[i]=线;
}
clm=randAry[i];
rep[row][clm].setAlive(true);
}
}
void World::nextGeneration(){
如果(环){
对于(int i=0;i cout您的单元格**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];
}
//int**rep=0;//将此
rep=新单元格*[l];
//为每列的元素分配的内存。
对于(int i=0;i
可能该单元格.h在world.h的include中大写。在类world data成员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];
}
World::World(intl,intc){
直线=l;
列=c;
环=假;
int**rep=0;
//为行元素分配的内存。
rep=新整数*[l];
//为每列的元素分配的内存。
对于(int i=0;i
起初在构造函数中我放了int**rep,这是一个键入错误。它必须是Cell**rep。现在我编辑了它!起初在构造函数中我放了int**rep,这是一个键入错误。它必须是Cell**rep。现在我编辑了它!如果是这样,编译器会引发一个错误,比如不声明Cell!写int而不是Cell是错误的当我输入问题时,输入错误。但是你已经正确使用了*谢谢!
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];
}