-3

I can't seem to figure out how to make the operator = with this vector or node and PCB. I know it an overload problem but everytime i make a different type of = it giving me some crazy error with vector and PCB and other stuff and its driving me crazy, so anyone got a hint on how i can create this so it won't crash or give me an error?

  • Assignment operator must be a member function. –  Dec 07 '16 at 05:51
  • i tried putting inside pcb but it crashes for me using bool operator = – darkflames363 Dec 07 '16 at 05:54
  • See [this answer](http://stackoverflow.com/a/4421719/434551) to [a highly voted question on SO](http://stackoverflow.com/questions/4421706/operator-overloading). – R Sahu Dec 07 '16 at 06:01
  • Also see [What is the copy-and-swap idiom?](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – R Sahu Dec 07 '16 at 06:04

1 Answers1

0

Here's the way I would do it:

struct PCB
{
[...]
   // assignment operator -- defining this operator isn't really necessary
   // for this particular class since all of the members of this class are
   // held by-value anyway (and thus the compiler-provided default
   // implementation would do exactly the same thing that this implementation
   // does) but I'm leaving it here anyway as example of what a properly
   // constructed assignment operator might look like.
   PCB& operator =(const PCB & b)
   {
      ProcessID     = b.ProcessID;
      ProcessorSize = b.ProcessorSize;
      priority      = b.priority;
      name          = b.name;
      return *this;
   }
};

[...]

printer_cpu[i] = PQ[i].top()->data; 
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • It isn't clear why you would want to implement the assignment operator. Maybe you could explain that? – juanchopanza Dec 07 '16 at 06:36
  • :O it worked but then it crashed im guessing at printer_cpu[i].top() part – darkflames363 Dec 07 '16 at 06:36
  • @juanchopanza The question was about implementing the assignment operator, so it seems like an example showing how to do it would be helpful. – Jeremy Friesner Dec 07 '16 at 06:48
  • @darkflames363 don't guess, find out for sure! (either by stepping through with a debugger, or by putting debug-prints after each line and seeing which one is the last one to be printed before the crash). In any case, one likely way to cause a crash would be if top() is returning a NULL pointer (since then trying to read the 'data' member item of NULL would cause a bad-memory-read error and a crash) – Jeremy Friesner Dec 07 '16 at 06:50
  • @JeremyFriesner It isn't clear what the question is about exactly, but in this case, the implementing the operator isn't needed at all. But your answer implies that it is. OP probably thinks they have to write it now. – juanchopanza Dec 07 '16 at 06:53
  • @juanchopanza agreed that the question is unclear, but the question starts out with "I can't seem to figure out how to make the operator =", so I showed how to make an operator=. Whether it's actually necessary or not from a code-correctness standpoint will depend on what the questioner wants to do with the PCB class in the future, but it's necessary by definition if the goal of the exercise is to specifically to learn how to write an operator=. – Jeremy Friesner Dec 07 '16 at 06:58
  • @JeremyFriesner You are misleading OP and others who don't know they don't have to implement an assignment operator in most cases, specifically this one. A less confusing "answer" would be "you don't have to implement an assignment operator in this case, so it isn't clear where your problem lies". Except that should be a comment. Obviously the operator doesn't help OP because it doesn't change the behaviour of the code. – juanchopanza Dec 07 '16 at 07:02
  • @juanchopanza well I'd hate to mislead anyone, so I've added a comment to the top of the operator= that explains the situation. – Jeremy Friesner Dec 07 '16 at 07:07
  • @JeremyFriesner it is the printer_cpu[i] i comment it out and it worked perfectly fine :S and when i put it back on it crashes – darkflames363 Dec 07 '16 at 07:26