I am getting a strange segmentation fault in my program. Dlist is a class that creates a linked list with operations to insert and remove items from a dynamic list. I am positive my implementation of this class is correct, but this code is producing a seg fault. The strange part is, when I make my atleastTwo and atleastOne functions pass by reference, the seg fault disappears and everything compiles. Can anyone shed some light on this problem?
bool atleastTwo(Dlist<double> stack){
try{
stack.removeFront();
stack.removeFront();
} catch(emptyList e){
cout << "Not enough operands\n";
return false;
}
return true;
}
bool atleastOne(Dlist<double> stack){
try{
stack.removeFront();
} catch(emptyList e){
cout << "Not enough operands\n";
return false;
}
return true;
}
void processInput(inputs usrInput, Dlist<double> &stack){
switch(usrInput){
case i_add:
if(atleastTwo(stack)){doOperation(stack, add);}
break;
case i_subtract:
if(atleastTwo(stack)){doOperation(stack, subtract);}
break;
case i_multiply:
if(atleastTwo(stack)){doOperation(stack, multiply);}
break;
case i_divide:
if(atleastTwo(stack)){doOperation(stack, divide);}
break;
case i_negation:
if(atleastOne(stack))negation(stack);
break;
case i_duplicate:
if(atleastOne(stack)){duplicate(stack);}
break;
case i_reverse:
if(atleastTwo(stack)){reverse(stack);}
break;
case i_print:
if(atleastOne(stack)){print(stack);}
break;
case i_clear:
clear(stack);
break;
case i_printAll:
printAll(stack);
break;
default:
break;
}
}
T *removeFront();
// MODIFIES this
// EFFECTS removes and returns first object from non-empty list
// throws an instance of emptyList if empty
Thanks