Hi everyone,

I just started doing C++ coming from a java background. I'm writing a discrete event simulator at the moment and need to put 'job' objects into a priority queue and sort them by their next job time- i.e. shortest job time first. I first tried doing the following: defining a container

priority_queue<Job,vector<Job*>,less<vector<Job*>::value_type> > event_list;

I got into a lot of trouble doing this because when I debugged the code I ended up with every element of the priority queue pointing to the same memory location. I am too inexperienced in c++ to fix this, so I tried:

priority_queue<Job,vector<Job>,less<vector<Job>::value_type> > event_list;

i.e. a priority queue of objects, not pointers. When I did this the compiler really didn't like it and brought up line 238 of vector.tcc and an error message:

"238 C:\Dev-Cpp\include\c++\3.4.2\bits\vector.tcc instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Source, _Alloc = std::allocator<Source>]' "

My question is: what should I do normally? Use pointers to objects in the container or objects? What does this error message mean?

Thanks, I'm just a novice c++ programmer so I'm sorry if this is dumb.

Recommended Answers

All 4 Replies

First: drop Dev-C++. Not only is the IDE itself outdated, but the version of MinGW that comes with it also is. Make sure to do a clean uninstall that also gets rid of MinGW, then install Code::Blocks.

As to your problem, store pointers if your objects are very expensive to copy or not copyable at all. However, when you do this, make sure to use a container that takes care of memory management, such as boost's pointer containers.

In other cases, store the object directly.
To tell what the problem with your code is, you need to post it.
But if I had to guess, I'd say you're trying to store a pointer in the non-pointer container.

Well this is one problem with:

priority_queue<Job,vector<Job*>,less<vector<Job*>::value_type> > event_list;

In this case, vector<Job*>::value_type is essentially equal to Job* (not exactly, but no need to worry about the difference). Then, now less<Job*>, really means that the elements will be sorted by the comparison "a < b" where a and b are of type Job*, so you will get a sorting by pointer address. I'm sure this is not what you want. So start reading from here, to know how to make something other than less<> for your purpose. So it should be (notice also the * in the first template argument:

priority_queue<Job*,vector<Job*>, some_user_defined_comparison_class > event_list;

>>I just started doing C++ coming from a java background
Welcome to the right side of the fence!

thanks very much - I'll get rid of dev-c++ asap. And I'll take that approach to storing objects in containers from now on.

I'll comment the code up a bit more clearly so it's readable to someone not familiar with the project I'm doing and then post it.

Well this is one problem with:

priority_queue<Job,vector<Job*>,less<vector<Job*>::value_type> > event_list;

In this case, vector<Job*>::value_type is essentially equal to Job* (not exactly, but no need to worry about the difference). Then, now less<Job*>, really means that the elements will be sorted by the comparison "a < b" where a and b are of type Job*, so you will get a sorting by pointer address. I'm sure this is not what you want. So start reading from here, to know how to make something other than less<> for your purpose. So it should be (notice also the * in the first template argument:

priority_queue<Job*,vector<Job*>, some_user_defined_comparison_class > event_list;

>>I just started doing C++ coming from a java background
Welcome to the right side of the fence!

Yes - I do relish the little challenges C++ throws up! And could probably talk for hours about how boring java makes life.

Thank you- I had thought about this but was not sure how to remedy it, in fact I had a suspicion this could be the problem when I was using the priority_queue of pointers- I must have been comparing the addresses or something. The code worked but it was clear from the log that I wasn't popping the Jobs I expected from the p_queue. I'll read the link now, much obliged.

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.