c++ - "Access violation reading error" -
exception thrown @ 0x003d65b6 in new_predator.exe: 0xc0000005: access violation reading location 0x00000000.
i keep getting error , cannot figure out why. here section of code points throwing error:
void simulation::run() { (int = 0; < 20; i++) { (int j = 0; j < 20; j++) { if (world[i][j]->name() == 'a') world[i][j]->move(); } } }
it points "if (world[i][j]->name() == 'a') line throwing error.
my simulation constructor defined follows:
this private variable in simulation class. critter abstract base class.
critter ***world; simulation::simulation() { world = new critter**[20]; (int = 0; < 20; i++) { world[i] = new critter*[20]; } (int = 0; < 20; i++) (int j = 0; j < 20; j++) world[i][j] = null; }
i have display function in sim class defined follows, similiar run function works fine:
void simulation::display() { (int = 0; < 20; i++) { std::cout << std::endl; (int j = 0; j < 20; j++) { if (world[i][j] == nullptr) std::cout << " . "; else if (world[i][j]->name() == 'a') std::cout << " o "; else std::cout << " x "; } } std::cout << std::endl; }
i have 2 derived class contain name member function returns char 'a' or 'd'. have ideas happening here? hope have provided enough information.
you must check word[i][j]
not nullptr
befor trying access it:
for (int = 0; < 20; i++) { (int j = 0; j < 20; j++) { if (word[i][j]!=nullptr && world[i][j]->name() == 'a') world[i][j]->move(); } }
by way, you're doing such check in display function :
if (world[i][j] == nullptr) // yes !!! std::cout << " . "; else if (world[i][j]->name() == 'a') // here we're sure it's not nullptr ...
Comments
Post a Comment