1

i've tried to multiply 2 large numbers represented as linked lilsts(overload the operator*) but there seems to be an error. could any of you help me, please ? i think that the multiply function is correct since i've tested it. but i can't seem to put the finger on what's wrong when i try to overload the operator*..... i've tried looping through one list and mutiply with each node from the other one.. extra a tip for dividing 2 large numbers ? thank you! here is my code:

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;
}

for full code https://pastebin.com/PcXuM9EL

qwerty2121
  • 13
  • 8
  • 1
    Just throwing a single function from your code at people and saying it doesn't work doesn't give them any useful information. There are many ways a linked list could be used to represent a "large number", and therefore many ways in which multiplication of those "large numbers" may be implemented. Voting to close as unclear. – Peter Mar 16 '19 at 11:34
  • @Peter is right, of course. Meanwhile, nevertheless, you might changing all your relevant `int` to `long long`. Depending on how big the numbers are, this might ameliorate your problem. However, it is possible to overflow `long long`, too. The `long long` type is usually 64 bits wide. – thb Mar 16 '19 at 11:39

1 Answers1

1

The problem is that this definition will not work for what you intend to do;

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

If you want to define a type Numar and overload arithmetic operators, you need to work on values (eventually const or rvalue references) and not on pointers. Otherwise, you'd leak memory as soon as you'd have some temporary calculations.

So you need to revise your code design, so that you'll end up with the following signature:

Numar Numar :: operator* (Numar nr2) 

For this to work, Numar and Lista need to implement the rule of 3.

Edit: In order to avoid copying values when it’s not necessary, you may want — as suggested by 1201programalarm in the comments — to go for:

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

But this may need some discipline in the definition of the member functions you invoke on nr2 in view of the const.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • That should probably be `Numar Numar :: operator* (const Numar &nr2)` to avoid making a copy of a `Numar` object, which looks like it would be expensive (at least one call to `new`). – 1201ProgramAlarm Mar 16 '19 at 16:55
  • @1201ProgramAlarm Yes, this would be a good idea indeed ! In the answer there’s a link to another question in which the usual signature for operator are discussed in more details. But I’ll edit to highlight this point – Christophe Mar 16 '19 at 17:02