In my program, I get segmentation fault when calling
cout << ( ball(1,0,0) ).getName();
For testing purposes when i call
( ball(1,0,0) ).getName();
I get an exception as expected and program doesn't crash.
What might be wrong here? I tried overloading ostream but it didn't seem to work. The related parts of the program are:
Ball& Field::operator()(int x, int y, int z){
try{
if (arr[x][y][z]==1){ // When it's 1, there is a ball in that coord
return search(root,x,y,z); // Return ball instance
}else{
throw ballException(); // 'Error: Ball not found'
}
}catch(std::exception &e){
std::cout << e.what() << std::endl;
}
}
const string& Ball::getName() const {
try{
return this->name;
}catch(std::exception & e){
}
}
Ball::Ball(const string& name, const string& type, int size){
this->name =name;
this->type = type;
this->size= size;
}
Ball exception is:
class ballException: public exception {
virtual const char* what() const throw() {
return "Error: No Ball in the coordinates!";
}
};