Problem with heap

A

Andre Paim Lemos

Hi,

I'm having some compiler problems when I try to use make_heap(),
push_heap() and pop_heap().
I am compiling my code on gcc version 3.3.1 (SuSE Linux).
I am using the heap related methods to create a priority queue on a
list of objects ordered by the objectId.
The problem is that the compiler says that I have to implement the
operator- on my object.

These are the code and the compiler errors:

CODE:


1 #include <algorithm>
2 #include <list>
3 #include <iostream>
4
5 #include <stdlib.h>
6 #include <time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 using namespace std;
11
12
13 class Test
14 {
15 private:
16 int id;
17 public:
18 Test(const int i) : id(i) {}
19 inline int getId() { return id; }
20 bool operator<(Test& other) { other.id <= id; }
21 friend Test operator-(Test& t1, Test& t2);
22 };
23
24 Test operator-(Test& t1, Test& t2)
25 {
26 int newid;
27 newid = t2.id - t1.id;
28 Test result(newid);
29 return(result);
30 }
31
32 int main()
33 {
34 list<Test> testList;
35
36 srand(time(NULL)+getpid());
37 for (int i = 0; i < 10; i++)
38 {
39 int id = 1+(int) (100.0*rand()/(RAND_MAX+1.0)); 38 {
39 int id = 1+(int) (100.0*rand()/(RAND_MAX+1.0));
40 cout << "Creating object - Id: " << id << endl;
41 Test t(id);
42 testList.push_back(t);
43 }
44
45 make_heap(testList.begin(), testList.end());
46
47 cout << "Printing Heap: "<< endl;
48
49 for (list<Test>::iterator i = testList.begin(); i !=
testList.end(); i++)
50 {
51 cout << " + Object Id: " << (*i).getId() << endl;
52 }
53
54 Test test(1+(int) (100.0*rand()/(RAND_MAX+1.0)));
55 testList.push_back(test);
56
57 cout << "Inserting Object into Heap - Id: " << test.getId()
<< endl;
58 push_heap(testList.begin(), testList.end());
59
60 cout << "Printing Heap: "<< endl;
61 for (list<Test>::iterator i = testList.begin(); i !=
testList.end(); i++)
62 {
63 cout << " + Object Id: " << (*i).getId() << endl;
64 }
65
66 pop_heap(testList.begin(), testList.end());
67 test = testList.back();
68 cout << "Removing Object from Heap - Id: " << test.getId()
<< endl;
69 testList.pop_back();
70
71 cout << "Printing Heap: "<< endl;
72 for (list<Test>::iterator i = testList.begin(); i !=
testList.end(); i++)
73 {
74 cout << " + Object Id: " << (*i).getId() << endl;
75 }
75 }
76
77 return(0);
78 }

COMPILER ERRORS:

andre@canis:/local_home/test/C++> g++ HeapTest.cc -o HeapTest
HeapTest.cc:39: error: `Test operator-()' must have an argument of
class or
enumerated type
HeapTest.cc:39: error: `Test operator-()' must take either one or two
arguments
/usr/include/g++/bits/stl_heap.h: In function `void
std::make_heap(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = std::_List_iterator<Test, Test&, Test*>]':
HeapTest.cc:56: instantiated from here
/usr/include/g++/bits/stl_heap.h:238: error: no match for 'operator-'
in '
__last - __first'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:239: error: no match for 'operator-'
in '
__last - __first'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:243: error: no match for 'operator+'
in '
__first + __parent'
/usr/include/g++/bits/stl_heap.h: In function `void
std::push_heap(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = std::_List_iterator<Test, Test&, Test*>]':
HeapTest.cc:69: instantiated from here
/usr/include/g++/bits/stl_heap.h:96: error: no match for 'operator-'
in '__last
- __first'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:96: error: no match for 'operator-'
in '__last
- 1'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h: In function `void
std::pop_heap(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = std::_List_iterator<Test, Test&, Test*>]':
HeapTest.cc:77: instantiated from here
/usr/include/g++/bits/stl_heap.h:175: error: no match for 'operator-'
in '
__last - 1'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:175: error: no match for 'operator-'
in '
__last - 1'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:175: error: no match for 'operator-'
in '
__last - 1'
HeapTest.cc:39: error: candidates are: Test operator-()
HeapTest.cc:31: error: Test operator-(Test&, Test&)

Any help would be Apreciated,
Andre
 
R

Rolf Magnus

Andre said:
Hi,

I'm having some compiler problems when I try to use make_heap(),
push_heap() and pop_heap().
I am compiling my code on gcc version 3.3.1 (SuSE Linux).
I am using the heap related methods to create a priority queue on a
list of objects ordered by the objectId.
The problem is that the compiler says that I have to implement the
operator- on my object.
....

24 Test operator-(Test& t1, Test& t2)
25 {
26 int newid;
27 newid = t2.id - t1.id;
28 Test result(newid);
29 return(result);
30 }

Read up on const correctness. Since your operator doesn't (and isn't
supposed to) modify its arguments, you should to write:

Test operator-(const Test& t1, const Test& t2)

This is probably producing the error.
 
R

Rolf Magnus

Andre said:
I did this and I'm still having the same compiler errors...

Unfortunately, your line numberings are not consistent with the error
messages. The first error message e.g. says:

HeapTest.cc:39: error: `Test operator-()' must have an argument of
class or enumerated type

but line 39 of your code doesn't contain any call to operator-. So it
seems to me that you didn't show the code that produced the errors.
 
A

Andre Paim Lemos

It's true, I'm sorry.
The correct error messages for the code I hava sent is:

andre@canis:/local_home/test/C++> g++ HeapTest.cc -o HeapTest
/usr/include/g++/bits/stl_heap.h: In function `void
std::make_heap(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = std::_List_iterator<Test, Test&, Test*>]':
HeapTest.cc:45: instantiated from here
/usr/include/g++/bits/stl_heap.h:238: error: no match for 'operator-' in '
__last - __first'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:239: error: no match for 'operator-' in '
__last - __first'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:243: error: no match for 'operator+' in '
__first + __parent'
/usr/include/g++/bits/stl_heap.h: In function `void
std::push_heap(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = std::_List_iterator<Test, Test&, Test*>]':
HeapTest.cc:58: instantiated from here
/usr/include/g++/bits/stl_heap.h:96: error: no match for 'operator-' in '__last
- __first'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:96: error: no match for 'operator-' in '__last
- 1'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h: In function `void
std::pop_heap(_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = std::_List_iterator<Test, Test&, Test*>]':
HeapTest.cc:66: instantiated from here
/usr/include/g++/bits/stl_heap.h:175: error: no match for 'operator-' in '
__last - 1'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:175: error: no match for 'operator-' in '
__last - 1'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
/usr/include/g++/bits/stl_heap.h:175: error: no match for 'operator-' in '
__last - 1'
HeapTest.cc:25: error: candidates are: Test operator-(Test&, Test&)
 

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,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top