• 0

Compiler Error


Question

I am having a compiler error when running my program. It opens pqueuebh.cpp(this is a priority queue class my teacher already wrote) and points to this line of the Insert code

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)

I am passing my Patient object. What I understand from the error code is that it has somethign to do with my < operator. But I already overloaded it. Here is my Patient class with my overloaded operator

class Patient

{

private:

int itsPatientNumber;

int itsPriority;

int itsTime;

int itsMisdiagnose;

public:

Patient();

~Patient();

void setPriority(int priority);

int getPriority();

void setTime(int time);

int getTime();

void setMisdiagnose(int misdiagnose);

int getMisdiagnose();

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;

}

};

Thanks.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

The compiler doesn't know how to compare your Patient class with whatever you're comparing it against. You have to manually write code for any operators you want to use with your classes. I can do this with C#, but it won't work with your c++ code :/

Link to comment
Share on other sites

  • 0

i am sorry if i misunderstood. but i thought that is why i overloaded the < operator? I shouldnt change the priority queue code. So is it my < operator overloading that I did something wrong with? could u tell me what i could do?

Link to comment
Share on other sites

  • 0

I am not sure where I find that out. I will post the whole Insert Code:

// Insert
//
// Amortized time complexity:
//
// ? ? ? ? ? O(1) ? ? if prioritization is NOT maintained
// ? ? ? ? ? O(log n) if prioritization is maintained for a
// ? ? ? ? ? ? ? ? ? ? ? Priority Queue that was already prioritized
// ? ? ? ? ? O(n) ? ? if prioritization is restored for a
// ? ? ? ? ? ? ? ? ? ? ? Priority Queue that was NOT prioritized

? ?template &lt;class Etype&gt;
? ?int
? ?PQueue&lt;Etype&gt;::Insert ( const Etype &amp; Element, int i )
? ?{
 ? i=1;
?  if ( CurrentSize == MaxSize )
 ? DoubleArray ( );
 ? Array[++CurrentSize] = Element; ?// Place Element at the end
 ? if ( OrderOK )
 ? if ( i != 1 )
 ? {
 ? ? ? if ( CurrentSize&gt;1 &amp;&amp; Element&lt;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;
 ? ?}

And here is the private members for the priority queue class:

// Data Members


// Current and maximum size of Priority Queue
int CurrentSize, MaxSize;

// 1 if heap-order property is preserved; 0 otherwise
int OrderOK;

// Dynamic binary heap to store the elements of Priority Queue
Etype *Array;

Link to comment
Share on other sites

  • 0

Ok. I got the answer from somebody else.

Replace

bool operator <(const Patient &P)

with

bool operator <(const Patient &P) const

that little const fixed it all. Thanks for the help though, really appreciate it!

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.