Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-10-2004, 04:45 PM   #1 (permalink)
saiz66
Registered User
 
Join Date: May 2004
Posts: 11
saiz66 is on a distinguished road
Compiler Error

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
Code:
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.
saiz66 is offline   Reply With Quote
Old 06-10-2004, 04:59 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I dont see where you passed your Patient code. Show it. Now I can only guess and I don't have time to make guesses. Perhpaps other collegues do have. Step in I'd say.

And think twice about your overloaded operator.
Basically it means: somePatient < otherPatient.
What does that mean actually? I should "intuitivly feel" what it means without looking at your implementation. Do you see what I mean?
Why not just test directly like:
Code:
somePatient.getPriority()< otherPatient.getPriority();
Anyway, there is nothting wrong with your class technically:
Code:
#include <iostream>
using namespace std;

class Patient
{
	
private:
	int itsPatientNumber;
	int itsPriority;
	int itsTime;
	int itsMisdiagnose;
	
public:
	Patient(){};
	~Patient(){};
public:
	void setPriority(int priority)
	{ itsPriority = priority; };
	int getPriority()
	{ return itsPriority; };
	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;
   }
};

int main()
{
	Patient pete, mary;
	pete.setPriority(2);
	mary.setPriority(3);
	
	if(mary<pete)
		cout<<"mary first!"<<endl;
	else 
		cout<<"pete first!"<<endl;

	return 0;
}
So you need to provide more info.
__________________
Valmont is offline   Reply With Quote
Old 06-10-2004, 05:09 PM   #3 (permalink)
saiz66
Registered User
 
Join Date: May 2004
Posts: 11
saiz66 is on a distinguished road
Sorry I will just paste my program.
Code:
#include "pqueuebh.cpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

};

Patient::Patient()
{
	itsPriority = itsTime = itsMisdiagnose = 0;
}


Patient::~Patient()
{
}

void Patient::setPriority(int priority)
{
	itsPriority = priority;
}

int Patient::getPriority()
{
	return itsPriority;
}

void Patient::setTime(int time)
{
	itsTime = time;
}

int Patient::getTime()
{
	return itsTime;
}

void Patient::setMisdiagnose(int misdiagnose)
{
	itsMisdiagnose = misdiagnose;
}

int Patient::getMisdiagnose()
{
	return itsMisdiagnose;
}

int main()
{
	
	using namespace std;
	using std::cout;
	using std::cin;
	
	PQueue <Patient> PQ;
	
	PQ.Insert(P1);

	return 0;
}
This is my code. And I implemented the priority queue which my prof gave me.

This is the header file which basically states what I need to pass and what it returns.

Code:
  #ifndef PQUEUEBH_HPP
   #define PQUEUEBH_HPP

   // Priority Queue: Header File

   template <class Etype>

   // Assumption: Etype is defined on the operation <

   class PQueue
   {
      public:

         // Constructor
         //
         // Input  : None
         // Purpose: To create an empty Priority Queue
         // Output : None

            PQueue ( );


         // Copy constructor
         //
         // Input  : Priority Queue PQ
         // Purpose: To initialize Priority Queue to PQ
         // Output : None

            PQueue ( const PQueue & PQ );


         // Destructor
         //
         // Input  : None
         // Purpose: To free memory of Priority Queue
         // Output : None

            ~PQueue ( );


         // Copy assignment
         //
         // Input  : Priority Queue PQ
         // Purpose: To assign PQ to current Priority Queue
         // Output : Current Priority Queue

            const PQueue & operator= ( const PQueue & PQ );


         // Insert
         //
         // Input  : Element E and integer i (optional)
         // Purpose: To place E into the Priority Queue and
         //          To maintain prioritization when i=1 or omitted
         // Note   : Prioritization may NOT be maintained when i!=1
         // Output : 1 if successful; 0 otherwise

            int Insert ( const Etype & E, int i=1 );


         // DeleteMin
         //
         // Input  : None
         // Purpose: To return the highest priority element E and
         //          To delete E from the Priority Queue
         // Output : Element E and 1 if successful; 0 otherwise

            int DeleteMin ( Etype & E );


         // Length
         //
         // Input  : None
         // Purpose: To the return the current size of the Priority Queue
         // Output : Current size of Priority Queue

            int Length ( ) const;


         // Empty
         //
         // Input  : None
         // Purpose: To check if Priority Queue is empty
         // Output : 1 if empty; 0 otherwise

            int Empty ( ) const;


         // Clear
         //
         // Input  : None
         // Purpose: To re-initialize Priority Queue to empty
         // Output : None

            void Clear ( );


      private:

         // DoubleArray
         //
         // Input  : None
         // Purpose: To double the maximum size of Priority Queue
         // Output : None

            void DoubleArray ( );


         // FixHeap
         //
         // Input  : None
         // Purpose: To restore the heap-order property
         //          of the binary heap
         // Output : None

            void FixHeap ( );


         // PercolateDown
         //
         // Input  : Position i
         // Purpose: To restore the heap-order property
         //          of the binary heap rooted at position i
         // Output : None

            void PercolateDown ( int i );


         // PercolateUp
         //
         // Input  : Position i
         // Purpose: To restore the heap-order property
         //          of the binary heap along the path from i to the root
         // Output : None

            void PercolateUp ( int i );


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

   };

   #endif
I was taught that I didn't need to touch the implementation file. Could you tell me what I am doing wrong?
saiz66 is offline   Reply With Quote
Old 06-10-2004, 05:12 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
*edit*
__________________
Valmont is offline   Reply With Quote
Old 06-10-2004, 05:18 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
And where exactly does it go wrong? Where did you use "<" ?
And feel free to post the cpp file as well.
__________________
Valmont is offline   Reply With Quote
Old 06-10-2004, 05:34 PM   #6 (permalink)
saiz66
Registered User
 
Join Date: May 2004
Posts: 11
saiz66 is on a distinguished road
I haven't used the < operator in my code yet. But what goes wrong is in the implementation file in my professor's code. I am happy to add it for you:

Code:
#include "pqueuebh.h"
#include <iostream.h>

// Priority Queue:  Implementation File ( Dynamic binary heap )

// Note: Delete and new functions are assumed to take constant time


// Initial size of Priority Queue
static const int InitPQSize = 10;


// DoubleArray
//
// Time complexity: O(n)

template <class Etype>
void
PQueue<Etype>::DoubleArray ( )
{
	Etype *OldArray = Array;
	Array = new Etype [2*MaxSize+1];
	for ( int i=1; i<=CurrentSize; i++ )
		Array[i] = OldArray[i];
	MaxSize *= 2;
	delete [] OldArray;
}


// FixHeap
//
// Time complexity: O(n)

template <class Etype>
void
PQueue<Etype>::FixHeap ( )
{
	for ( int i=CurrentSize/2; i>0; i-- )
		PercolateDown ( i );
	OrderOK = 1;
}


// PercolateDown
//
// Time complexity: O(log n)

template <class Etype>
void
PQueue<Etype>::PercolateDown ( int Parent )
{
	int Child = 2*Parent;
	Etype Temp = Array[Parent];
	while ( Child <= CurrentSize )
	{
		
		if (    Child != CurrentSize            // Right child exists
			&& Array[Child+1] < Array[Child] )
			Child++;
		
		if ( Array[Child] < Temp )
		{
			Array [Parent] = Array[Child];
			Parent = Child;
			Child = 2*Parent;
		}
		else
			break;
	}
	Array[Parent] = Temp;
}


// PercolateUp
//
// Time complexity: O(log n)

template <class Etype>
void
PQueue<Etype>::PercolateUp ( int Child )
{
	int Parent = Child/2;
	Etype Temp = Array[Child];
	while ( Parent > 0 )
	{
		if ( Temp < Array[Parent] )
		{
			Array[Child] = Array[Parent];
			Child = Parent;
			Parent = Child/2;
		}
		else
			break;
	}
	Array[Child] = Temp;
}


// Constructor
//
// Time complexity: O(1)

template <class Etype>
PQueue<Etype>:: PQueue ( ) : MaxSize ( InitPQSize ),
CurrentSize ( 0 ),
OrderOK ( 1 )
{
	// Root is defined at position 1 (and NOT 0)
	Array = new Etype [MaxSize+1];
}


// Copy constructor
//
// Time complexity: O(n)

template <class Etype>
PQueue<Etype>::PQueue ( const PQueue<Etype> & Rhs )
{
	Array = NULL;
	*this = Rhs;
}


// Destructor
//
// Time complexity: O(1)

template <class Etype>
PQueue<Etype>::~PQueue ( )
{
	delete [ ] Array;
}


// Copy assignment
//
// Time complexity: O(n)

template <class Etype>
const PQueue<Etype> &
PQueue<Etype>::operator= ( const PQueue<Etype> & Rhs )
{
	if ( this != &Rhs )
	{
		delete [ ] Array;
		Array = new Etype [Rhs.MaxSize+1];
		MaxSize = Rhs.MaxSize;
		CurrentSize = Rhs.CurrentSize;
		OrderOK = Rhs.OrderOK;
		for ( int i=1; i<=CurrentSize; i++ )
			Array[i] = Rhs.Array[i];
	}
	return *this;
}


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


// DeleteMin
//
// Time complexity:
//
//           O(log n)   if the Priority Queue was already prioritized
//           O(n)       otherwise

template <class Etype>
int
PQueue<Etype>::DeleteMin ( Etype & Element )
{
	if ( Empty ( ) )
	{
		return 0;
	}
	
	// Prioritize the binary heap if necessary
	if ( !OrderOK )
		FixHeap ( );
	
	Element = Array[1];
	Array[1] = Array[CurrentSize--];
	PercolateDown ( 1 );   // Maintain heap-order property
	return 1;
}


// Length
//
// Time complexity: O(1)

template <class Etype>
int
PQueue<Etype>::Length ( ) const
{
	return CurrentSize;
}


// Empty
//
// Time complexity: O(1)

template <class Etype>
int
PQueue<Etype>::Empty ( ) const
{
	return CurrentSize == 0;
}


// Clear
//
// Time complexity: O(1)

template <class Etype>
void
PQueue<Etype>::Clear ( )
{
	CurrentSize = 0;
	OrderOK = 1;
}
I highlited the part where it shows the compiler error. Thanks for the help.
saiz66 is offline   Reply With Quote
Old 06-10-2004, 05:48 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll look into it.
__________________
Valmont is offline   Reply With Quote
Old 06-10-2004, 06:03 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Uuhhm wait a minute...
How about you overloading operator< OUTSIDE the Patient class? Do you see why?
Code:
#ifndef PATIENT_H
#define PATIENT_H

class Patient
{	
public:
	Patient(){};
	~Patient(){};
	friend bool operator<(const Patient &T, const Patient &P);
public:
	void setPriority(int priority)
	{ itsPriority = priority; };
	int getPriority()
	{ return itsPriority; };
	void setTime(int time);
	int getTime();
	void setMisdiagnose(int misdiagnose);
	int getMisdiagnose();
private:
	int itsPatientNumber;
	int itsPriority;
	int itsTime;
	int itsMisdiagnose;	
};

bool operator <(const Patient &T, const Patient &P)
{
	if (T.itsPriority < P.itsPriority)
		return true;
	else if (T.itsPriority > P.itsPriority)
		return false;
	else if (T.itsTime < P.itsTime)
		return false;
	else 
		return true;
}

#endif //PATIENT_H
Do you also understand what the friend keyword means?
Also learn to place private(s) last in the class declaration. Details last
__________________

Last edited by Valmont; 06-10-2004 at 06:47 PM.
Valmont is offline   Reply With Quote
Old 06-10-2004, 06:27 PM   #9 (permalink)
saiz66
Registered User
 
Join Date: May 2004
Posts: 11
saiz66 is on a distinguished road
hmm.. does that work? well if it does, I found another solution:

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!
saiz66 is offline   Reply With Quote
Old 06-10-2004, 06:44 PM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I didn't provide you this solution on purpose:
Binary comparison operators should be defined as global functions.
Why? Because this way you never have to worry about the order of arguments.

In my previous post I asked you to think about it .
Now you know.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is this error? john_tran Standard C, C++ 4 10-20-2004 08:30 PM
Compiler Comparison Fred Standard C, C++ 4 05-17-2004 03:17 PM
C++ compile error Amaranthine Standard C, C++ 5 05-05-2004 08:23 PM


All times are GMT -8. The time now is 09:26 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting