乘以两个表示为链接列表的大数字



我尝试将2个表示为链接的lilsts(Operation*)表示的大数字,但似乎存在错误。你们中有人可以帮我吗?我认为自从我对其进行了测试以来,乘法功能是正确的。但是当我试图超载操作员*.....我尝试在一个列表中循环循环,然后用一个节点与另一个节点进行互相循环。谢谢你!这是我的代码:

Numar *Numar :: operator* (Numar *nr2) //overloading operator*
{
Lista *L = new Lista;
Numar *rezultat = new Numar(L);//links the list to the number 
Lista *aux = new Lista;
Numar *rez2 = new Numar(aux); //an auxiliary 
int t = 1;
Nod *p2 = this->L->prim; //1st node of this
while (p2) //loop the 2nd number
{
rez2 = nr2->multiply(p2->info * t); //multiply the 1st list with an int
cout<<"rez2 "<<rez2;
rezultat = *rezultat + rez2;
cout<<"rezultat "<<rezultat;
t *= 10; //that carry 
p2 = p2->next;
}
return rezultat;
}

有关完整代码https://pastebin.com/pcxum9el

问题是,此定义对您打算做的事情不起作用;

Numar *Numar :: operator* (Numar *nr2) 

如果要定义Numar类型和过载算术运算符,则需要在值(最终或RVALUE引用)而不是指针上进行工作。否则,您将在进行一些临时计算后立即泄漏内存。

因此您需要修改代码设计,以便最终获得以下签名:

Numar Numar :: operator* (Numar nr2) 

为此,NumarLista需要实施3。

的规则

编辑:为了避免在不需要的情况下复制值,您可能希望 - 如1201 Programprammararm在评论中所建议的,以:

Numar Numar :: operator* (const Numar& nr2) 

,这可能需要在您在NR2上调用的成员函数的定义中进行一些纪律,鉴于const。

最新更新