AdamSpurgin
AdamSpurgin

Reputation: 961

Iterating over an std::map, Doing something wrong, complaining of a missing operator for '=' for the iterator

Here's my function:

friend std::ostream& operator<< (std::ostream& stream, const Path& path) {
    std::map<double, glm::vec3>::iterator iter;
    for (iter = path.points.begin(); iter != path.points.end(); iter++){
        stream << "test" << "\n";
    }
}

And here's my error:

Error   1   error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>' (or there is no acceptable conversion) c:\users\adam\skydrive\documents\proj\ray\ray\path.h    22  1   ray

I've never had this kind of problem before. And to be honest, I don't know where to start. I've tried a few methods of getting an iterator, including the typedef method, but the same problem persists.

Any advice?

Upvotes: 1

Views: 304

Answers (2)

user3553031
user3553031

Reputation: 6224

Path is const, so you're using the const version of .begin(). But you're trying to assign it to a mutable iterator. Try declaring iter as Path::const_iterator instead.

Upvotes: 6

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

Chamge this statement

std::map<double, glm::vec3>::iterator iter;

to

std::map<double, glm::vec3>::const_iterator iter;

Upvotes: 2

Related Questions