• 0

c++ class question


Question

Hi. I basically have to create a Hospital program. I created a Patient class which holds each of the patient's priority, treatment time and so on. Well I made those variables private and created a getPriority, setPriority methods in public. Those look like this:

void setPriority(int priority);

int getPriority();

That seems to be fine. Then I have to insert into a priority queue, which has already been created by my professor. I have then created a Priority Queue:

PQueue <Patient> PQ;

Ok. then I inserted the patient class into the priority queue:

PQ.Insert(patient);

And this is where I get confused. I want to get the patients with the highest priority and print them out. The function to get the highest priority is:

int DeleteMin ( Etype & E );

I understand that it returns the highest priority in Etype & E where the Etype is class Patient. I tried this:

PQ.DeleteMin(patient);

I am confused. Do I need to create a new class. Anybody can help me out? Thanks in advance!

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

You need to do a PQ.DeleteMin(&patient); as I'm guessing it expects a pointer. patient is the same as *(patient + i), and as you can see it dereferences, so thus you're sending it a value (or in this case an object), not a pointer (or pointer to an object).

Link to comment
Share on other sites

  • 0
I thought that Etype & E was a reference, and that it sent the answer back to the variable E?

Possibly, but I guess I'm trying to understand your problem. What do you get when you try to do PQ.DeleteMin(patient);? What exactly is the problem?

I am confused. Do I need to create a new class.

What are you confused about? Create a new class to do what?

Link to comment
Share on other sites

  • 0

Sorry for not being clear. My professor has given me the code for the priority queue. I know how to insert integers into it. And what DeleteMin(const Et0ype &E) does is delete the top of the priority and passes it into the variable u passed. So for example if I did a PQ.DeleteMin(priority) the top priority would be placed into the variable priority. Now I have to do that with a class called Patient which I created. I made an array of Patients because I have to create 25 Patients with various priorities. I tried to insert it into the Priority Queue. It gave me an error:

if ( CurrentSize>1 && Element<Array[CurrentSize/2] )

error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class Patient' (or there is no acceptable conversion)

the whole function is this:

template <class Etype>

int

PQueue<Etype>::Insert ( const Etype & Element, int i )

{

i=1;

if ( CurrentSize == MaxSize )

DoubleArray ( );

Array[++CurrentSize] = Element; // Place Element at the end

if ( OrderOK )

if ( i != 1 )

{

if ( CurrentSize>1 && Element<Array[CurrentSize/2] )

OrderOK = 0; // Heap-order property is violated

}

else

PercolateUp ( CurrentSize ); // Maintain heap-order property

else

if ( i == 1 )

FixHeap ( ); // Restore heap-order property

return 1;

}

I have already overloaded my < operator

bool operator <(const Patient &P)

{

if (itsPriority < P.itsPriority)

return true;

else if (itsPriority > P.itsPriority)

return false;

else if (itsTime < P.itsTime)

return false;

else

return true;

}

I have put this in the public part of patient. I dont know what is wrong.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.