c++ - Passing iterator by reference gives an error: cannot convert parameter 1 from 'const data' to 'data & -


i have function int func(data& dt). its need use list of structureslst. its proposed map , list has elements (but in example has 1 element). have iterate through list , push each element func function reference.

#include <iostream> #include <map> #include <algorithm> #include <list> #include <string>  typedef struct{     char inf[3]; }data;  std::map<int, data*> mp; std::list<data> lst;  int func(data& dt){      ... }  int main(){       ...     //iterate through list     std::list<data>::const_iterator iterator;      (iterator = lst.begin(); iterator != lst.end(); ++iterator) {         data param = *iterator;         func(param);          //func(*iterator); // doesn`t work!     }      return 0; } 

the problem is: func doesnt work *iterator. why should this:data param = *iterator` make work? thank help!

iterator const_iterator:

std::list<data>::const_iterator iterator; 

but function try pass target expects non-const reference:

int func(data& dt){ 

a const_iterator cannot give non-const reference. need either use non const std::list<data>::iterator, or change function work const data& parameter. on surface, looks there no requirement parameter non-const reference.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -