tags:

views:

49

answers:

3

Hi,

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

The error is in the line signed with: LINE WITH ERROR

What I'm I doing wrong, how should I change it?

Luis ...............................

$ g++ -c b.h 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: b.h

#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
A: 

always avoid

for(..;..;..); { }
stefaanv
I would use a word stronger than *avoid* here ;)
Billy ONeal
+6  A: 

You left a semicolon after for():

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

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

Cubbi
+1  A: 

$6.5.3/3 - "If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for statement."

Therefore iter cannot be accessed outside the for loop scope. Check the semicolon immediately after the for loop. Most probably you did not intend it that way.

Chubsdad
No, I didn't intend to use the ';'.Thank you all.Luis
Luis