The search for the name 'iter' has changed for the new ISO definition for 'for'

I am trying to compile the two files below, but I get an error message from the compiler: gcc 4.3.3 (Linux)

Error in line signed with: LINE WITH ERROR

What am I doing wrong, how can I change it?

Louis ...............................

$ g++ -c bh b.cpp b.cpp: In function 'void calcularDesempPop(std::vector<Individuo, std::allocator<Individuo> >&)': b.cpp:19: error: name lookup of 'iter' changed for new ISO 'for' scoping b.cpp:17: error: using obsolete binding at 'iter' 

............................... FILE: b.cpp

 #include <iostream> #include <algorithm> #include "desempenho.h" using std::cout; using std::endl; struct Individuo { vector<double> vec; double desempenho; }; void calcularDesempPop(vector<Individuo>& pop) { for (vector<Individuo>::iterator iter = pop.begin(); iter != pop.end(); ++iter);//LINE WITH ERROR iter->desempenho = calcularDesempenho(iter->vec); cout << endl; } 

............................... FILE: bh

 #ifndef GUARD_populacao_h #define GUARD_populacao_h //#include <algorithm> #include <iostream> #include "cromossoma.h" struct Individuo { vector<double> vec; double desempenho; }; void calcularDesempPop(vector<Individuo>& pop); #endif 
+4
source share
3 answers

$ 6.5.3 / 3 - "If the for-init statement is a declaration, the volume name (s) are declared to the end for approval."

Therefore, it cannot be accessed outside the scope of the for loop. Check the semicolon immediately after the for loop. Most likely, you did not want this.

+11
source

You left a semicolon after for ():

 for (vector::iterator iter = pop.begin(); iter != pop.end(); ++iter); 

which means that the next line is not part of the body of the loop, and iter is undefined.

+8
source

always avoid

 for(..;..;..); { } 
+1
source

All Articles