最初,我的实验室传递了三个参数: addFractionJesseR(*lFrac, *rFrac, **resFrac); 但我刚刚发现我不能传递三个参数。我不得不将其更改为 **resFrac = addFractionJesseR(*lFrac, *rFrac); 现在我在编译时遇到问题。我知道我的指针和双指针在某个地方超出了范围,但我就是不知道在哪里。调试器指向第二行作为问题:
FractionJesseR& FractionJesseR::operator=(const FractionJesseR& arg) {
num = arg.num;
denom = arg.denom;
return *this;
}
由以下方式调用:
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); 最初是 addFractionJesseR(*lFrac, *rFrac, **resFrac);
由以下方式调用:
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) 最初是 addMenu(lFrac, rFrac, &resFrac)
(是的,我确实对我的所有指针都调用了 delete,我还是 Stack Overflow 的新手,并且正在学习只放置相关的代码片段)我需要帮助来指引我正确的方向。我认为我的指针超出了 addMenu 或 displayMenu 某处的范围......也许我取消引用了一个错误的双指针?
任何帮助将不胜感激!
编辑:
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;
}