RECHERCHER :
COMMUNAUTE MP
Identifiez vous ...
Devenir Membre
J'ai oublié mon MDP
DOMAINE MP
Bavardages
Langages Généraux
Langages Web
Langages DotNet
Autres langages
Dev. Jeux Video
Sécurité
Sys. Exploitation
Graphismes
Logiciels
Réseaux
Bases de données
Méthodologies
Emplois High-tech
Aide juridique
Articles juridiques
FORUM
Index des forums
Ajouter un sujet
Rechercher sujet
Contact Responsable
Devenir modérateur
CHAT MP IRC
Votre pseudo ...
Serv: irc.irc-land.org
Chan: #MoteurProg
PARTICIPER
Plus de 3500 emplois.
Rechercher un job
Déposez votre CV
Emplois High-tech

Visiteur MP

 redéfinition d'opérateurs

Forum : C++
Sous Catégorie : Aucune
Type du sujet : Sujet Normale
FAQ : FAQ C++

SUIVI DES SUJETS PAR MAIL

SUIVI PAR MAIL INACTIF

RESOLUTION DU SUJET SUJET NON RESOLU
BLOQUAGE DU SUJET SUJET ACTIF
APPARTENANCE A LA FAQ N'APPARTIENT PAS A LA FAQ


PAGE : [1]

POSTER UN NOUVEAU SUJET REPONDRE A CE SUJET

FORUM C++

PREMIERE PAGE

PAGE PRECEDENTE

Page précedente

Page suivante

PAGE SUIVANTE

DERNIERE PAGE
eklips
Nouveau membre
Inscrit : 19/10/2007
Messages : 1
Message
#144285
Posté le 19/10/07 à 21:28
Bonjour à tous..
J'ai un problème avec un travail scolaire. J'ai écris un programme qui utilise une file de priorite à la facon d'un spooler. Tout semble fonctionner mais j'ai ces erreurs à la compilation:

/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp ]’:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_heap.h:279: instantiated from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator ]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_heap.h:404: instantiated from ‘void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator ]’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_queue.h:353: instantiated from ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp ]’
../main.cpp:81: instantiated from here
/usr/lib/gcc/i586-mandriva-linux-gnu/4.2.2/../../../../include/c++/4.2.2/bits/stl_function.h:227: erreur: no match for ‘operator<’ in ‘__x < __y’
../main.cpp:62: note: candidats sont: bool operator<(Tache&, Tache&)
make: *** [main.o] Erreur 1

Il semblerait que la redéfinition des opérateurs ne fonctionne pas et ni moi ni mon professeur ne trouvons pourquoi..

Si ca vous tente de défier un prof du cegep vous êtes les bienvenus

voici mon code:


#include <iostream>
#include <queue>
#include <functional>

using namespace std;

class Tache{

public:

Tache(void) { nom = "sans nom", priorite = 1 ; }

Tache( string leNom, int laPriorite ){
if(setNom(leNom)){
if(setPriorite(laPriorite))
else setPriorite(1);
}
else setNom("sans nom");
}


bool setNom( string nom ){return(nom.size()>0);}

bool setPriorite(int prio){return (prio<5 && prio>0);}

string getNom(){return nom;}

int getPriorite(){return priorite;} const

~Tache(){}

private:
string nom;

int priorite;


};

bool operator<( Tache & tache1, Tache & tache2 ){
return tache1.getPriorite() < tache2.getPriorite() ;
}

bool operator==(Tache & tache1, Tache & tache2 ){
return tache1.getPriorite() == tache2.getPriorite() ;
}

bool operator>( Tache & tache1, Tache & tache2 ){
return tache1.getPriorite() > tache2.getPriorite() ;
}

int main( ){

/** var: char Option, Choix de l'utilisateur */
char option;
/** var: string optionSaisie, le string lu lors de la saisie du choix*/
string optionSaisie;

priority_queue< Tache, deque<Tache>, less<Tache> > spool ;

bool stop = false;



cout << "Lancement de l'application ..." << endl;

do
{
// Le menu
cout << endl;
cout << "----------------------------------------" << endl;
cout << " a => Ajouter une tache d'impression." << endl;
cout << " e => Enlever une tache d'impression." << endl;
cout << " i => Imprimer le prochain élément." << endl;
cout << " f => Afficher le contenu de la file d'impression" << endl;
cout << " s => Suspendre l'impression." << endl;
cout << " r => Reprendre l'impression." << endl;
cout << " q => Quitter." << endl;
cout << "------------------------------------------" <<endl;
do
{
cout << "Choix: " << endl ;
getline(cin, optionSaisie);
if(optionSaisie.size()== 0 ) // si vide reprendre la saisie
continue ;
option=optionSaisie.at(0); // le premier caractere de la chaine lue



}while (((string) "aeifsrq").find(option) == string::npos);

// Aussi longtemps que le caractere choisi n'est pas trouve dans les
// caracteres valides
cout << endl;


switch (option)
{
//menu test du tableau indice
case 'a':
{
string nom;
string priorite;
int prio;
int taille = spool.size() ;
int cnt = 1 ;
bool ok = true;
Tache val;
priority_queue< Tache, deque<Tache> > temp ;

cout<<"quel est le nom de la tache a ajouter?"<<endl;
getline(cin,nom);

while( cnt <= taille ){
val = spool.top() ;
if(val.getNom() != nom){
spool.pop() ;
temp.push(val) ;
cnt++ ;
}
else ok = false;
}
while(cnt>=1){
spool.push(temp.top());
temp.pop();
cnt--;
}
if(ok){
cout<<"Quel est la priorite de la tache (1-4)"<<endl;
getline(cin,priorite);
prio = (int)priorite[0]-48;

if(prio > 0 && prio < 5){
spool.push(Tache(nom,prio));
cout<<"Tache insere correctement"<<endl;
}
else
cout<<"priorite invalide, veuillez essayer de nouveau"<<endl;

}
else{
cout<<"Impossible d'utiliser ce nom, soit la file est pleine,"<<endl;
cout<<"soit le nom existe deja"<<endl;
}
}
break;

//menu test du tableau de pointeurs
case 'e':
{
Tache val;
int taille = spool.size() ;
int cnt = 1 ;
bool ok = false;
string nom;
priority_queue<Tache, deque<Tache> > temp ;

cout<<"Quel est le nom de la tache que vous voulez enlever?"<<endl;
getline(cin,nom);

while(cnt <= taille){
val = spool.top() ;
if(val.getNom() != nom){
spool.pop() ;
temp.push(val) ;
cnt++ ;
}
else{
spool.pop();
cout<<"La tache a ete enleve correctement"<<endl;
ok = true;
}
}
while(cnt>=1){
spool.push(temp.top());
temp.pop();
cnt--;
}
if(!ok)
cout<<"La tache n'existe pas, veuillez essayer de nouveau"<<endl;
}
break;

// Menu comparer deux chaines
case 'i':{
if(stop = false){
if(spool.size()>0 ){
Tache val;
val = spool.top();
cout<<"impression de "<<val.getNom()<<" "<<val.getPriorite()<<endl;
spool.pop();
}
else cout<< "La file d'impression est vide"<<endl;
}
else cout<<"Impossible d'imprimer en ce moment"<<endl;
}
break;

// Menu phrases alléatoires
case 'f':{
Tache val;
int taille = spool.size() ;
int cnt = 1 ;
priority_queue< Tache, deque<Tache> > temp ;

while( cnt <= taille ){
val = spool.top() ;
cout<<val.getNom()<<" "<<val.getPriorite()<<endl;
spool.pop() ;
temp.push(val) ;
cnt++ ;
}
while(cnt>=1){
spool.push(temp.top());
temp.pop();
cnt--;
}
}
break;

case 's':{
stop = true;
}
break;

case 'r':{
stop = true;
}
break;

// Menu Quitter
case 'q':
cout << "Arret du programme..." << endl;
break;

}

}while (option != 'q');


return 0;
}

HAUT DE PAGE

PROFIL MEMBRE LUI ECRIRE 

Publicité
Inscrit : X
Messages : X
Message
#Aucun

HAUT DE PAGE

  

tupapau
Nouveau membre
Inscrit : 28/11/2004
Messages : 23
Message
#144300
Posté le 20/10/07 à 13:16
Je sais pas si ça répond exactement à ton problème mais voilà un bout de code trouvé en 10s avec google code search qui montre comment implémenter une priority_queue avec les opérations de comparaison.


// rak - Rakshasa's toolbox // Copyright (C) 2005-2006, Jari Sundell // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // In addition, as a special exception, the copyright holders give // permission to link the code of portions of this program with the // OpenSSL library under certain conditions as described in each // individual source file, and distribute linked combinations // including the two. // // You must obey the GNU General Public License in all respects for // all of the code used other than OpenSSL. If you modify file(s) // with this exception, you may extend this exception to your version // of the file(s), but you are not obligated to do so. If you do not // wish to do so, delete this exception statement from your version. // If you delete this exception statement from all source files in the // program, then also delete it here. // // Contact: Jari Sundell <jaris@ifi.uio.no> // // Skomakerveien 33 // 3185 Skoppum, NORWAY #ifndef RAK_PRIORITY_QUEUE_DEFAULT_H #define RAK_PRIORITY_QUEUE_DEFAULT_H #include <stdexcept> #include <rak/functional.h> #include <rak/functional_fun.h> #include <rak/priority_queue.h> #include <rak/timer.h> namespace rak { class priority_item { public: priority_item() {} ~priority_item() { if (is_queued()) throw std::logic_error("priority_item::~priority_item() called on a queued item."); m_time = timer(); m_slot.set(NULL); } bool is_valid() const { return m_slot.is_valid(); } bool is_queued() const { return m_time != timer(); } void call() { m_slot(); } void set_slot(function0<void>::base_type* s) { m_slot.set(s); } const timer& time() const { return m_time; } void clear_time() { m_time = timer(); } void set_time(const timer& t) { m_time = t; } bool compare(const timer& t) const { return m_time > t; } private: priority_item(const priority_item&); void operator = (const priority_item&); timer m_time; function0<void> m_slot; }; struct priority_compare { bool operator () (const priority_item* const p1, const priority_item* const p2) const { return p1->time() > p2->time(); } }; typedef std::equal_to<priority_item*> priority_equal; typedef priority_queue<priority_item*, priority_compare, priority_equal> priority_queue_default; inline void priority_queue_perform(priority_queue_default* queue, timer t) { while (!queue->empty() && queue->top()->time() <= t) { priority_item* v = queue->top(); queue->pop(); v->clear_time(); v->call(); } } inline void priority_queue_insert(priority_queue_default* queue, priority_item* item, timer t) { if (t == timer()) throw std::logic_error("priority_queue_insert(...) received a bad timer."); if (!item->is_valid()) throw std::logic_error("priority_queue_insert(...) called on an invalid item."); if (item->is_queued()) throw std::logic_error("priority_queue_insert(...) called on an already queued item."); if (queue->find(item) != queue->end()) throw std::logic_error("priority_queue_insert(...) item found in queue."); item->set_time(t); queue->push(item); } inline void priority_queue_erase(priority_queue_default* queue, priority_item* item) { if (!item->is_queued()) return; // Check is_valid() after is_queued() so that it is safe to call // erase on untouched instances. if (!item->is_valid()) throw std::logic_error("priority_queue_erase(...) called on an invalid item."); // Clear time before erasing to force it to the top. item->clear_time(); if (!queue->erase(item)) throw std::logic_error("priority_queue_erase(...) could not find item in queue."); if (queue->find(item) != queue->end()) throw std::logic_error("priority_queue_erase(...) item still in queue."); } } #endif

HAUT DE PAGE

PROFIL MEMBRE LUI ECRIRE 

Alp
Superviseur :
- Système d'ex.
- Méthodologie.
- C & C++
Modérateur :
- Bavardages
Chef de projet(s) :
- My SDL Lib

Avatar de Alp
Inscrit : 24/06/2004
Messages : 2547
Message
#144308
Posté le 20/10/07 à 15:43
Déjà il y a un const mal placé dans la définition de ta classe, et puis tu écris tes codes avec un syntaxe correcte mais très étrange. Essaye de formater tout ça de manière plus conventionnelle... du genre :

if(fonctionOk()) { // je fais qqch } else { // ... }

entre autres...

Ca serait déjà pas mal.
Pour ton const, c'est à cette ligne, je te laisse trouver l'erreur :

int getPriorite(){return priorite;} const


Pour ton opérateur, afin de trouver l'erreur, il faudrait déjà régler ce qui est lié à la syntaxe et puis ajouter des balises pour le code (le bouton CODE en haut quand tu écris un message), nous donner les lignes où ça foire de manière claire ... En gros nous donner les informations de manières plus précises et claires.

Merci pour tes efforts, ça payera ;)
__________________________
Be C++
Mon Blog (C++, Intelligence Artificielle, Prolog)

HAUT DE PAGE

PROFIL MEMBRE LUI ECRIRE 
POSTER UN NOUVEAU SUJET REPONDRE A CE SUJET

PREMIERE PAGE

PAGE PRECEDENTE Page précédente

Page suivante

PAGE SUIVANTE DERNIERE PAGE

FORUM C++



    PAGE : [1]



.: Site Web développé par Julien Pichot et l'équipe MPWG avec www.evolvia-web.com :.