Sign in to follow this  

priority_queue "const" problems (i think)

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a priority queue that I am adding nodes to... here is my code class Node { public: Node::Node(); Node::Node(int data, int weight); Node::Node(Node *leftChild, Node *rightChild); Node::~Node(); void Node::traverse(); bool operator > (const Node &n) const { return this->weight > n.weight; } public: int data; int weight; Node *leftChild; Node *rightChild; }; When I call the traverse() method on a node: tree.top().traverse(); I get a compile time error: error: passing ‘const Node’ as ‘this’ argument of ‘void Node::traverse()’ discards qualifiers

Share this post


Link to post
Share on other sites
priority_queue::top() returns a constant reference to the top element. This makes sense, because if you could change it willy nilly, it might stop being the largest item.

If traverse can't be made a const member function, but you know it won't affect the order, one alternative would be to drop the priority_queue all together in favor of a regular old sorted list of some sort. See make_heap, pop_heap, and push_heap for one possibility.

CM

Share this post


Link to post
Share on other sites
ok, so obviously I'm not that versed with "const" functions, but making the traverse() function into "void traverse() const" solved my compile error. We'll see if that works. Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by harmon
ok, so obviously I'm not that versed with "const" functions, but making the traverse() function into "void traverse() const" solved my compile error. We'll see if that works. Thanks


It worked because the compiler now knows that you won't be altering the object that you are calling the method of.

For example if you had a class MyClass with a method MyMethod, then you can create an object of MyClass that is const or not const. If it's a const object, then you can't call any functions that will modify the internals of the MyClass object. And the only way for the compiler to know that MyMethod doesn't modify the internals of a MyClass object is by specifying that MyMethod is const.


class MyClass {
void MyMethod() {}
};

const MyClass c_m;
MyClass m;

c_m.MyMethod(); // fail because c_m is a constant.
m.MyMethod(); // ok


Changing void MyMethod() to void MyMethod const; will allow c_m to use that method.

have a good read on this

Share this post


Link to post
Share on other sites
Two things.

First off, you don't need that Node:: in front of those function definitions within the class. That is only needed when the methods are declared outside of the class.

Second: Your > operator would be better defined as:
friend const bool operator > (const Node &lhs, const Node &rhs)
{
return lhs.weight > rhs.weight;
}
(Still within the class though, in this case)

btw, the 'const bool' helps pick up errors such as:
if (a > b = aIsGreater)
where you meant to use ==

Declaring the function as friend and making both parameters explicit makes both sides convertable to type 'Node' as appropriate, which can make certain code easier to write later.

Share this post


Link to post
Share on other sites

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this