PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > What's wrong with this code?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
What's wrong with this code?

Réponse
 
LinkBack Outils de la discussion
Vieux 04/04/2008, 18h27   #1
Kelvin Moss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut What's wrong with this code?

Hi all,

I was trying to use priority queues and wrote a sample code. g++
accepted the code but Comeau online doesn't. I can't figure out what
Comeau is complaining about. Could anyone please suggest?

#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;

struct Person {
string name;
int age;
Person(string n, int i):name(n), age(i)
{}
};

struct Comp {
bool operator()(const Person &p1, const Person &p2) {
if (p1.age > p2.age) {
return true;
} else {
return false;
}
}
};

int main()
{
priority_queue<Person, vector<Person>, Comp> q;

q.push(Person("XXX", 28));
q.push(Person("BBBB", 42));
q.push(Person("CCC", 75));
q.push(Person("DDD", 51));

while (!q.empty()) {
cout << q.top().name << endl;
q.pop();
}
}

Comeau cribs like --
"sequence_concepts.h", line 31: error: no instance of constructor
"Person::Person" matches the argument list
typename _XX::value_type __t = typename _XX::value_type();
^
detected during:
instantiation of "void

_ERROR_IN_STL_SEQ::__fill_constructor_requirement_ violati
on(_XX &) [with _XX=std::vector<Person,
std::allocator<Person>>]" at line 164
instantiation of "void

_Sequence_concept_specification<_Sequence>::_Seque nce_req
uirement_violation(_Sequence) [with
_Sequence=std::vector<Person,
std::allocator<Person>>]"
at line 26 of "ComeauTest.c"

May be I am missing something?

TIA ..
  Réponse avec citation
Vieux 04/04/2008, 18h42   #2
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's wrong with this code?

Kelvin Moss wrote:

> struct Person {
> string name;
> int age;
> Person(string n, int i):name(n), age(i)
> {}


> Comeau cribs like --
> "sequence_concepts.h", line 31: error: no instance of constructor
> "Person::Person" matches the argument list
> typename _XX::value_type __t = typename _XX::value_type();


> May be I am missing something?


Yes. You are missing a default constructor.
The compiler only generates one if you don't have user-defined constructors
in your class.

  Réponse avec citation
Vieux 04/04/2008, 18h59   #3
Kelvin Moss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's wrong with this code?

On Apr 4, 10:42 am, Rolf Magnus <ramag...@t-online.de> wrote:
> Kelvin Moss wrote:
> > struct Person {
> > string name;
> > int age;
> > Person(string n, int i):name(n), age(i)
> > {}
> > Comeau cribs like --
> > "sequence_concepts.h", line 31: error: no instance of constructor
> > "Person::Person" matches the argument list
> > typename _XX::value_type __t = typename _XX::value_type();
> > May be I am missing something?

>
> Yes. You are missing a default constructor.


Yes, that fixes the problem.
> The compiler only generates one if you don't have user-defined constructors
> in your class.


I know this thing. So I need the default constructor because of STL
requirements? I know this might be compiler specific but can you tell
me where default construction might be taking place here?

Thanks!

  Réponse avec citation
Vieux 04/04/2008, 19h07   #4
outlaw
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's wrong with this code?

> Hi all,
>
> I was trying to use priority queues and wrote a sample code. g++
> accepted the code but Comeau online doesn't. I can't figure out what
> Comeau is complaining about. Could anyone please suggest?


My (_MSC_VER 1400) VStudio 2005 doesn't complain...

marioc@computer.org
http://securitymario.spaces.live.com/
  Réponse avec citation
Vieux 04/04/2008, 19h22   #5
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's wrong with this code?

* Kelvin Moss:
> On Apr 4, 10:42 am, Rolf Magnus <ramag...@t-online.de> wrote:
>> Kelvin Moss wrote:
>>> struct Person {
>>> string name;
>>> int age;
>>> Person(string n, int i):name(n), age(i)
>>> {}
>>> Comeau cribs like --
>>> "sequence_concepts.h", line 31: error: no instance of constructor
>>> "Person::Person" matches the argument list
>>> typename _XX::value_type __t = typename _XX::value_type();
>>> May be I am missing something?

>> Yes. You are missing a default constructor.

>
> Yes, that fixes the problem.
>> The compiler only generates one if you don't have user-defined constructors
>> in your class.

>
> I know this thing. So I need the default constructor because of STL
> requirements? I know this might be compiler specific but can you tell
> me where default construction might be taking place here?


As far as I can recall, Comeau is wrong here. The container element type needs
to be copy constructable and assignable (and, curiously, not redefine the
address operator to yield anything but the object's address), but that's it, as
far as I can recall.

Maybe someone can check the standard (I'm not in the mood).

But it sure looks like Comeau is very very wrong.


Cheers, & hth.,

- Alf
  Réponse avec citation
Vieux 04/04/2008, 19h53   #6
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's wrong with this code?

"Alf P. Steinbach" <alfps@start.no> wrote in
news:ga6dncXs55AR7GvanZ2dnUVZ_qainZ2d@comnet:

>> I know this thing. So I need the default constructor because of STL
>> requirements? I know this might be compiler specific but can you tell
>> me where default construction might be taking place here?

>
> As far as I can recall, Comeau is wrong here. The container element
> type needs to be copy constructable and assignable (and, curiously,
> not redefine the address operator to yield anything but the object's
> address), but that's it, as far as I can recall.
>
> Maybe someone can check the standard (I'm not in the mood).
>
> But it sure looks like Comeau is very very wrong.
>


I know that if you don't have a default constructor, there are several
methods that require you to provide them with an instance to use as the
default value. (Methods like resize() for example) I wonder if the
priority_queue adaptor is doing something funny to you in that regard.

joe
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12448 seconds with 14 queries