0
votes

using CPP map ,get warning info from eclipse editor.

Point find_shortest_node(const vector<Point> &openset,const   map<Point, int> &f_score,const map<Point, vector<int> > &f_direction) {

vector<Point>::iterator iner_iterator = openset.begin();
Point min_point = *iner_iterator;
while (iner_iterator != openset.end()) {
    if (f_score[*iner_iterator] < f_score[min_point]) {
        min_point = *iner_iterator;
    } 
        else if (f_score[*iner_iterator] == f_score[min_point]) {
            vector<int> temp1 = f_direction[*iner_iterator], temp2 =f_direction[min_point];
            if (temp1.size() < temp2.size()) {
            min_point = *iner_iterator;
            continue;
        }
    }
    iner_iterator++;
}
return min_point;

}

warning info:

passing 'const std::map' as 'this' argument of '_Tp& std::map<_Key, _Tp,_Compare, _Alloc>::operator[](const _KT&) [with _KT = Point, _Key = Point, _Tp = int,_Compare = std::less, _Alloc = std::allocator >]'discards qualifiers [-fpermissive]

2
I don't see the map reference declared as const in your code but I still suspect that this is the problem stackoverflow.com/questions/1474894/…ales_t
It looks suspiciously like you removed a const keyword from before the second argument when posting this. Are you sure there isn't a const there in your real code?Joseph Mansfield

2 Answers

1
votes

For some reason Eclipse thinks that f_score is const. According to your declaration, it is not const, so this looks like a problem with Eclipse's editor.

If you have a C++11 - compliant compiler, you can work around this problem by using map::at instead of the square brackets [] operator, like this:

while (iner_iterator != openset.end()) {
    if (f_score.at(*iner_iterator) < f_score.at(min_point)) {
        min_point = *iner_iterator;
    } else if (f_score.at(*iner_iterator) == f_score.at(min_point)) {
            vector<int> temp1 = f_direction.at(*iner_iterator), temp2 =f_direction.at(min_point);
            if (temp1.size() < temp2.size()) {
            min_point = *iner_iterator;
            continue;
        }
    }
    iner_iterator++;
}
0
votes

The simplest solution is to replace

f_score[something]

with

f_score.find(something)->second

This works on const references as it does not create a new element when something is not found. You should check whether the element was actually found though:

map<Point, int>::const_iterator it = f_score.find(something);
if (it != f_score.end()) {
    // ok, something was found
} else {
    // element not found
}