|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 .. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
> 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/ |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
* 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"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 |
|
![]() |
| Outils de la discussion | |
|
|