I want to insert and order 3 objects (from different classes) in a priority queue.

I create an array to access to the element inside the priority and then I push in the priority queue.

The code:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class Alumnos{
	public:
		string nombre;
		int edad;
		string operativo;
		
		Alumnos( string nombre, int edad, string oper ){
			this -> nombre = nombre;
			this -> edad = edad;
			this -> operativo = oper;
		};
		virtual void verificarEdad(){};
	
};

class Nuevo : public Alumnos{
	public:
		Nuevo( string nombre, int edad, string oper) : Alumnos( nombre,edad, oper ){};
	
		void verificarEdad(){
			cout << "Nuevo" << endl;
		};
};

class Medio : public Alumnos{
	public:
		Medio( string nombre, int edad, string oper ) : Alumnos ( nombre, edad, oper ){};
		
		void verificarEdad(){
			cout << "Medio" << endl;
		};
};

class Viejo : public Alumnos{
	public:
		Viejo( string nombre, int edad, string oper ) : Alumnos ( nombre, edad, oper ){};
		
		void verificarEdad(){
			cout << "Viejo" << endl;
		};
};

bool operator > (Alumnos alumno1, Alumnos alumno2)
{
	return alumno1.edad > alumno2.edad;
}

int main ()
{
	Nuevo *alumnoNuevo[4];
	Medio *alumnoMedio[4];
	Viejo *alumnoViejo[4];
	
	priority_queue<Alumnos*, vector<Alumnos*>, greater<vector<Alumnos*>::value_type> > mypq;
	
	alumnoMedio[0] = new Medio("Helder", 25, "activo");
	alumnoNuevo[0] = new Nuevo("Pepe", 18, "activo");
	alumnoViejo[0] = new Viejo("Pepito", 31, "activo");
	alumnoMedio[1] = new Medio("Juan", 21, "activo");
	
// 	nuevo n1("Helder", 18);
// 	alumnos &al = n1;
// 	al.verificarEdad();
	// 	
// 	medio m1("Pepe", 25);
// 	alumnos &al1 = m1;
// 	al1.verificarEdad();

	mypq.push(alumnoMedio[0]);
	mypq.push(alumnoNuevo[0]);
	mypq.push(alumnoViejo[0]);
	mypq.push(alumnoMedio[1]);

	alumnoMedio[1] -> operativo = "no Activo";
	
	Alumnos *al = mypq.top();
	
	for( int i = 0; i < 4; i++ ){
		al -> verificarEdad();
		cout << "mypq.top() is now " << al -> edad << endl;
		mypq.pop();
// 		mypq.push(al);
		al = mypq.top();
	}
	
/* 
*	C A M B I A R  E L  E S T A D O
*/ 
	
// 	alumnoMedio[0] -> operativo = "no Activo";
// 	cout << endl << alumnoMedio[0] -> operativo << endl << endl;
	
	return 0;
}

The exit is:

Medio
mypq.top() is now 25
Nuevo
mypq.top() is now 18
Viejo
mypq.top() is now 31
Medio
mypq.top() is now 21

This is the order I inserted in the queue.

The class operator > is correctly called and the code is correct?
What should I do to insert objects and ordered them with my code?

Regrettably you did not pay attention to the class Apointer in my post in your previous thread on this topic:
http://www.daniweb.com/forums/thread144299.html
The point is that you have a priority_queue<Alumnos*> now, not priority_queue<Alumnos>. So queue ordering (priority) is defined by operator>() for pointers to Alumnos, not by Alumnos::operator>(). But pointer values ordering does not bear a relation to Alumnos ordering (by edad member values).

Declare wrapper class for pointers to Alumnos with correspondent operator>() and declare priority_queue for this class. Look at my example in the previous thread.

Thanks for your precious help.

I thought I had my problem solved, but now I realized that the operator > in my program doesn't work.

I look at my previous post but I can't understand were I have to touch my code to put working the operator > working.

bool operator > (const Alumnos& alumno1, const Alumnos& alumno2)
{
	cout << ">" << endl;
	return alumno1.edad > alumno2.edad;
}

The previous code is my suggestion, but it doesn't work.

I think I have to correct to right way this line or should I change lines more of my code?

Finally I found the solution :D :

I have to declare a struct wich implement the comparison not the operator >.

struct CompareFooPtr : public std::binary_function<Alumnos*, Alumnos*, bool>
{
	bool operator()(Alumnos* al1, Alumnos* al2)
	{
		return al1 -> edad > al2 -> edad;
	}
};

priority_queue<Alumnos*, vector<Alumnos*>, CompareFooPtr > mypq;

Many thanks for your help.

Yes, it's one of possible solutions.
It's a matter of tastes: I prefer to declare class wrapper for pointers.
Congratulations!
Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.