1

If you notice in the following functions they both have the same for loop that searches for a integer location. Pop() compiles but I get an error for top() having to do with the const qualifiers. The heap class inherits from eecs281heap which stores a functor Comp compare where Comp is the typename. The instructor told us the only way to access the functor is through this->() so i'm just lookin for some guidance here. Thanks

error: passing ‘const larger’ as ‘this’ argument of ‘bool larger::operator()(int, int)’ discards qualifiers

This happens after running the following in int main. Through testing I already know the constructor works properly.

vector <int> data={10,2,13};
poorman_heap<int,larger> y(data.begin(),data.end());

template<typename TYPE, typename COMP>
void poorman_heap<TYPE, COMP>::pop() {
    int location=0;
    for(int i=1;i<data.size();i++){
        if(this->compare(data.at(i),data.at(location))){
            location=i;
        }
    }
    data.erase(data.begin()+location);
    return;
}

template<typename TYPE, typename COMP>
const TYPE& poorman_heap<TYPE, COMP>::top() const {
    int location=0;
    for(int i=1;i<data.size();i++){
        if(this->compare(data.at(i),data.at(location))){
            location=i;
        }
    }
    return data.at(location); 
}

P.S. greater is

struct greater{
    bool operator()(int x,int y){
        return x>y;
    }
}
4
  • 1
    Could it be that compare is not const? Commented Oct 25, 2013 at 5:53
  • i thought that was the case but i'm not allowed to change any of the class compilations so when i pass in the greater functor for compare, i don't know how to access it with top() Commented Oct 25, 2013 at 5:54
  • It doesn't make much sense. Are you saying this->compare is a greater instance? If so, your code is broken because it assumes TYPE is int. But you can create a local greater instance on the fly, that allows you to call non-const methods on it. Commented Oct 25, 2013 at 5:57
  • my constructor initializes a functor accessed through this->compare which i pass greater than to. When using it in other methods, it works properly and sorts appropiately. Keep in mind, any functor can be passed into the class to store but in this case i chose greater. I'm just having trouble accessing this functor in the top() class if that makes more sense Commented Oct 25, 2013 at 6:00

2 Answers 2

1

Make the call operator of greater a const operator:

struct greater
{
    bool operator()(int x,int y) const
    {
        return x>y;
    }
}

The same applies to whatever this->compare resolves to. It needs to be const.

It doesn't make much sense for a comparator to be non-const.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like the problem is that the compare member has operator() declared as a non-const function. Since it sounds like you don't have the ability to change that, you might be able to get the behavior that you want by declaring it as a mutable member in poorman_heap.

The mutable keyword lets you distinguish between an object being "physically const" (meaning the actual bytes don't change and being "logically const" (meaning the bytes might change but the value of the object isn't different in a fundamental sense). Basically it means that something "doesn't count" for the purposes of const-ness. The classic example in my mind is lazy initialization - you want to declare the get_value() function on a class const, but you also don't want to waste time computing the value if no one uses it, so you declare the value mutable and now you're allowed to calculate it and assign to it inside get_value() even though it is a const member function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.