This has stumped me for a few hours now, since I cannot see any problem in the math or code. (Dispite staring at it and working it out over and over again to be sure.) I’m hoping you folks can help me, here’s my code:
#define SOLVE_POSITION(x, y, z) ( z*16 + y*4 + x )
std::bitset<64> block;
block.reset();
for(int z = 0; z < 4; ++z){
for(int y = 0; y < 4; ++y){
for(int x = 0; x < 4; ++x){
if(block.at(SOLVE_POSITION(3-x, y, 3-z))){ //<-- call to at() throws 'out_of_range'
// do stuff
};
};
};
};
With z being 0, the two inner most for loops run their course entirely (for a total of 16 passes.) However, once z becomes 1, that’s when the exception is thrown from within std::bitset<64>::at().
The values of z, y, x are respectively 1, 0, 0 at that moment.
Can you tell me what is happening here to cause this exception?
Thanks in advance!
Macros! You have to be really careful about this:
You define:
so when you do:
it expands to:
and because of operator precedence,
3-x*16will be incorrect! You need to do:so that it expands correctly to:
as expected.