473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

stl priority queue of objects

hi, there

Can someone please tell me what's wrong with the following code? I
used to use the boost::shared_ptr in myUnit2 class and works fine, now
I take out the boost ptr and use normal pointer and got compiler
error. Please help.

-------------------in .h

class myUnit2 {
public:
double delta;
int id;
myUnit2(const double delta, const int id);
~myUnit2();

bool operator<(const myUnit2& right) const {
return delta < right.delta;
};
};

template <typename Pointer>
struct ptr_maxOnTop2{
bool operator()(const myUnit2& lhs, const myUnit2& rhs){
return lhs < rhs ;
}
};

-----------------------in .cpp

std::priority_queue<myUnit2* , std::vector< myUnit2* >, ptr_maxOnTop2<
myUnit2* priorityQ;

-----------------------error message
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_heap.h: In function `void
std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp,
_Compare) [with _RandomAccessIterator =
__gnu_cxx::__normal_iterator<myUnit2**, std::vector<myUnit2*,
std::allocator<myUnit2* >, _Distance = int, _Tp = myUnit2*,
_Compare = ptr_minOnTop2<myUnit2*>]':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_heap.h:404: instantiated from `void
std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare)
[with _RandomAccessIterator = __gnu_cxx::__normal_iterator<myUnit2**,
std::vector<myUnit2*, std::allocator<myUnit2* >, _Compare =
ptr_minOnTop2<myUnit2*>]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_queue.h:369: instantiated from `std::priority_queue<_Tp,
_Sequence, _Compare>::priority_queue(const _Compare&, const
_Sequence&) [with _Tp = myUnit2, _Sequence = std::vector<myUnit2*,
std::allocator<myUnit2*, _Compare = ptr_minOnTop2<myUnit2*>]'

zl2k

Oct 12 '07 #1
2 2542
zl2k wrote:
hi, there

Can someone please tell me what's wrong with the following code? I
used to use the boost::shared_ptr in myUnit2 class and works fine, now
I take out the boost ptr and use normal pointer and got compiler
error. Please help.

-------------------in .h

class myUnit2 {
public:
double delta;
int id;
myUnit2(const double delta, const int id);
~myUnit2();

bool operator<(const myUnit2& right) const {
return delta < right.delta;
};
};

template <typename Pointer>
struct ptr_maxOnTop2{
bool operator()(const myUnit2& lhs, const myUnit2& rhs){
return lhs < rhs ;
}
I venture the conjecture that you wanted this to be:

bool operator()( Pointer lhs, Pointer rhs){
return (*lhs) < (*rhs) ;
}
};

-----------------------in .cpp

std::priority_queue<myUnit2* , std::vector< myUnit2* >, ptr_maxOnTop2<
myUnit2* priorityQ;
You do not supply a comparison functor for pointers but for pointees.
[snip]

Best
Kai-Uwe Bux
Oct 12 '07 #2
On Oct 11, 10:19 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
zl2k wrote:
hi, there
Can someone please tell me what's wrong with the following code? I
used to use the boost::shared_ptr in myUnit2 class and works fine, now
I take out the boost ptr and use normal pointer and got compiler
error. Please help.
-------------------in .h
class myUnit2 {
public:
double delta;
int id;
myUnit2(const double delta, const int id);
~myUnit2();
bool operator<(const myUnit2& right) const {
return delta < right.delta;
};
};
template <typename Pointer>
struct ptr_maxOnTop2{
bool operator()(const myUnit2& lhs, const myUnit2& rhs){
return lhs < rhs ;
}

I venture the conjecture that you wanted this to be:

bool operator()( Pointer lhs, Pointer rhs){
return (*lhs) < (*rhs) ;
}
};
-----------------------in .cpp
std::priority_queue<myUnit2* , std::vector< myUnit2* >, ptr_maxOnTop2<
myUnit2* priorityQ;

You do not supply a comparison functor for pointers but for pointees.

[snip]

Best
Kai-Uwe Bux
Thanks, it fixed the problem.

Oct 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
6
by: Der Andere | last post by:
Are priority queues implemented in the STL in Visual Studio 6? If no, which is the easiest way to simulate them? Multisets? BTW: The container contains pointers to instances of a class. The...
1
by: Der Andere | last post by:
I posted this reaction in a thread quite far below so I fear people won't read it. I got stuck now for quite a while and just don't know what to try else. Thanks, Matthias > > Are priority...
5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
25
by: vooose | last post by:
Suppose execution of a particular thread T1 hits Monitor.Enter(obj); //critical section and blocks at the first line. (ie someone else is in the critical section) Now suppose more threads...
3
by: Erik | last post by:
Hi Everyone, I was thinking of how to do this for a while now and I cant get my head round it so I though I'd ask the experts! :-) What I am doing is reading in large amounts of data over TCP...
3
by: Chris Lasher | last post by:
Hello, I am working with large graphs (~150,000 to 500,000 nodes) which I need decompose node-by-node, in order of a node's value. A node's value is determined by the sum of its edge weights....
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
4
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.