We've been given test cases and one of the test cases is here you have to make sure that the user doesn't input a negative size for the 2D array. I'm not really sure what to do to protect against it. This is my code for that portion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Puzzle::Puzzle(const Puzzle& rhs){
m_size = rhs.m_size;
if(rhs.m_size > 0){
makeMem();
for(int i = 0; i < m_size; i++){
for(int j = 0; j < m_size; j++){
m_table[i][j] = rhs.m_table[i][j];
}
}
}
//If user input size is less than 0
if (rhs.m_size < 0){
m_table = nullptr;
m_size = 0;
}
}
I'm still getting a "terminate called after throwing an instance of 'std::bad_array_new_length'
what(): std::bad_array_new_length" error - which I'm assuming is from the test input being negative.
(2) You're showing a copy constructor, so this presumably means that the original object being copied had a size < 0. Maybe one solution is to prevent this from happening (handle the error before it even gets to the point where a copy constructor is being called)?