c++ - segmentation fault setting private data -
i have 7 x 7 matrix of probes deliver signal representing level measured @ point in area under investigation. each element of matrix cpp (g++) class , segmentation fault when try set private data element rect.x element of sdl_rect value.
class probe { public: probe(); ~probe(); void setcolor(int x); void set_id(int x, int y, int z); void level_id(void); private: int oldcolor; int xcord; int ycord; sdl_rect rect; }; //when access rec using public functions, rect.x returns 0 //and setting value give segment fault @ run time. //ie. rect.x = xcord; //segment fault foo = rect.x; // returns 0, starting value suppose. //my workround create rect locally in each public function sdl_rect rect = {block_w * xcord, block_h * ycord, block_w, block_h}; //and think should be. i'm not stuck. ian //more details //#include etc probe level[7][7]; //49 instances of class void probe::set_id(int x, int y, int z) { xcord = x; //segment fault ycord = y; oldcolor = z; // defininlg rectangles sdl_rect rect = {block_w * xcord, block_h * ycord, block_w, block_h}; // rect.x = block_w * xcord; //segment fault // rect.y = block_h * ycord; sdl_setrenderdrawcolor( ren, oldcolor, 0, 0, 255 ); sdl_renderfillrect( ren, &rect ); // if (0 != sdl_fillrect(surface, &rect, sdl_maprgb(surface->format, oldcolor, 0, 0))); // { std::cout << "sdl_fillrect failed in set_id" << std::endl; } } void sdl_init() { int x, y, z; z = 10; for(y = 0; y <= 7; ++y) { for(x = 0; x <= 7; ++x) { z += 25; level[x][y].set_id(x, y, z); } } for(y = 0; y < 7; ++y) { for(x = 0; x < 7; ++x) { level[x][y].level_id(); } } } void mouthpad() { std::cout << "in mouthpad" << std::endl; //start sdl , create window if( !init() ) { printf( "failed initialize!\n" ); } else { sdl_init(); //get class going. //main loop flag bool quit = false; //event handler sdl_event e; //etc hope helps. ian.
Comments
Post a Comment