What's wrong with this code?

K

Kelvin Moss

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>::_Sequence_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

Rolf Magnus

Kelvin said:
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.
 
K

Kelvin Moss

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!
 
J

Joe Greer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top