Originally, my lab was passing three argument: addFractionJesseR(*lFrac, *rFrac, **resFrac); but I just found out I can't pass three arguments. I had to change it to **resFrac = addFractionJesseR(*lFrac, *rFrac); and now I'm having problems compiling. I know my pointers and double pointers are out of scope somewhere but I just can't spot where. the debugger points to the second line as the problem:
FractionJesseR& FractionJesseR::operator=(const FractionJesseR& arg) {
num = arg.num;
denom = arg.denom;
return *this;
}
which is called by:
FractionJesseR& addMenu(FractionJesseR* lFrac, FractionJesseR* rFrac) {
int option;
FractionJesseR** resFrac = new FractionJesseR*();
......
case 2:
cout << "Calling add() --\n\n";
**resFrac = addFractionJesseR(*lFrac, *rFrac);
break;
......
**resFrac = addFractionJesseR(*lFrac, *rFrac); was originally addFractionJesseR(*lFrac, *rFrac, **resFrac);
which is called by:
void displayMenu() {
int option;
FractionJesseR *lFrac = nullptr;
FractionJesseR *rFrac = nullptr;
FractionJesseR *resFrac = nullptr;
......
case 2:
cout << " Adding Option --\n\n";
if (lFrac == nullptr && rFrac == nullptr) {
cout << " Not a proper call as no Fractions are available!\n\n";
}
else {
*resFrac = addMenu(lFrac, rFrac);
}
break;
*resFrac = addMenu(lFrac, rFrac) was originally addMenu(lFrac, rFrac, &resFrac)
(yes, I did call delete on all my pointers, I'm still new to Stack Overflow and learning to only put up relevant snippets of code) I need help pointing me in the right direction. I think my pointers go out of scope somewhere in addMenu or displayMenu... maybe I'm dereferencing a double pointer wrong?
Any help would be greatly appreciated!
edit:
FractionJesseR& addFractionJesseR(FractionJesseR& lFrac, FractionJesseR& rFrac) {
int n = 0;
int d = 0;
FractionJesseR *resFrac = nullptr;
// Adding the fractions
n = (&lFrac)->getNum() * (&rFrac)->getDenom() + (&lFrac)->getDenom() *
(&rFrac)->getNum();
d = (&lFrac)->getDenom() * (&rFrac)->getDenom();
resFrac = new FractionJesseR(n / gcd(n, d), d / gcd(n, d));
if (d < 0) {
d = -d;
n = -n;
}
return *resFrac;
}