c++ - Cannot access class member of a 2d array of object pointers (type***) -
i making conway's game of life. have 2 classes, 1 plane of cells, , cells. cells 2d linked list 4 pointers per cell pointing vertical , horizontal neighbors. when trying access of cell pointers other cells, or alive member program crashes.
my code
//game.h #ifndef game_h_ #define game_h_ #include <iostream> class cell{ public: bool alive; cell* top; cell* bot; cell* lef; cell* rig; cell(){ alive = false; top = bot = lef = rig = nullptr; } cell* link(char); int alive_neighbors(); void link_right(cell*); void link_down(cell*); void refresh_cell(); }; class field{ public: int size; cell * origin; bool ** new_state; cell *** fi; field(int a); ~field(); }; #endif
and
//game.cpp #include <iostream> #include "game.h" int cell::alive_neighbors(){ int num = 0; (this->top)?((this->top->alive)?(++num):(num)||((this->top->rig)?((this->top->rig->alive)?(num++):(num)):(num))):(num); (this->bot)?((this->bot->alive)?(++num):(num)||((this->bot->lef)?((this->bot->lef->alive)?(num++):(num)):(num))):(num); (this->rig)?((this->rig->alive)?(++num):(num)||((this->rig->bot)?((this->rig->bot->alive)?(num++):(num)):(num))):(num); (this->lef)?((this->lef->alive)?(++num):(num)||((this->lef->bot)?((this->lef->bot->alive)?(num++):(num)):(num))):(num); return num; } void cell::link_right(cell* linkee){ this->rig = linkee; linkee->lef = this; } void cell::link_down(cell* linkee){ this->bot = linkee; linkee->top = this; } field::field(int a){ size = a; (int i= 0; < size; i++){ fi[i] = new cell*[size]; (int j = 0; j < size; j++){ fi[i][j] = new cell; } } (int = 0; < size; i++){ (int j = 0; j < size -1; j++){ this->fi[i][j]->link_right(this->fi[i][j+1]); this->fi[j][i]->link_down(this->fi[j+1][i]); } } origin = fi[0][0] } field::~field(){ (int = size -1; >= 0; i--){ (int j = size -1;j >= 0; j--){ delete fi[i][j]; } delete fi[i]; } }
error:
#include "game.h" int main(){ field game(10); std::cout << game.origin->alive << std::endl; //compiles crashes :( std::cout << game.origin->rig << std::endl; //also compiles , crashes without giving adress. std::cout << game.fi[0][0]->alive; //even directly accessing cell compiles , crashes. }
the problem game.cpp
try code
//game.cpp #include <iostream> #include "stackoverflow.hpp" int cell::alive_neighbors(){ int num = 0; (this->top)?((this->top->alive)?(++num):(num)||((this->top->rig)?((this->top->rig->alive)?(num++):(num)):(num))):(num); (this->bot)?((this->bot->alive)?(++num):(num)||((this->bot->lef)?((this->bot->lef->alive)?(num++):(num)):(num))):(num); (this->rig)?((this->rig->alive)?(++num):(num)||((this->rig->bot)?((this->rig->bot->alive)?(num++):(num)):(num))):(num); (this->lef)?((this->lef->alive)?(++num):(num)||((this->lef->bot)?((this->lef->bot->alive)?(num++):(num)):(num))):(num); return num; } void cell::link_right(cell* linkee){ this->rig = linkee; linkee->lef = this; } void cell::link_down(cell* linkee){ this->bot = linkee; linkee->top = this; } field::field(int a){ size = a; fi = new cell**[size]; (int i= 0; < size; i++){ fi[i] = new cell*[size]; (int j = 0; j < size; j++){ fi[i][j] = new cell; } } (int = 0; < size; i++){ (int j = 0; j < size -1; j++){ this->fi[i][j]->link_right(this->fi[i][j+1]); this->fi[j][i]->link_down(this->fi[j+1][i]); } } origin = fi[0][0]; } field::~field(){ (int = size -1; >= 0; i--){ (int j = size -1;j >= 0; j--){ delete fi[i][j]; } delete fi[i]; } }
i not understand why people here tend annoyed because ones asking questions not coding "correct" style. need make mistakes , learn them. on note in future when work on c++ project. try using stl containers, refreshingly simple , efficient. you'll love them.
also think of n-dimensional arrays way. use 1 *
every dimension. int* arr
1-d array , int** arr
2-d array. can switch cell**
here since want 2-d array :)
Comments
Post a Comment