0

in a method, I have a local variabile

std::list<proiezione> ora;    

and a member variable of same type

std::list<proiezione> orb;   

In my method I have

for (std::list<proiezione>::iterator it = ora.begin(); it != ora.end(); ++it)
    this->orb.push_back(*it);

but doesn't work!

this->mem is empty! why?

precisely:

class CMFCApplication4Doc : public CDocument
{
public:
std::map<CString, Esame> esami;
INT valore_lode;
proiezione pr;
std::list<proiezione> orb;
    void get_proiezione(FLOAT media_desiderata);
}

void CMFCApplication4Doc::get_proiezione(FLOAT   media_desiderata)
{
    std::list<proiezione> ora;
std::vector<CString> v_proiezione;
CString appoggio;
std::map<CString, Esame> es = esami;
calcola_proiezione(ora,&pr, es, media_desiderata,valore_lode);
for (std::list<proiezione>::iterator it = ora.begin(); it != ora.end(); ++it)
    this->orb.push_back(*it);
 ecc ecc (I don't touch orb anymore)

} 

in debug mode I have "ora" with 25 elements, but "this->orb" with zero elements !

volperossa
  • 1,339
  • 20
  • 33

1 Answers1

1

Why not use the std::copy algorithm?

std::copy(tList.begin(), tList.end(), std::back_inserter(mem));

Or use copy-swap:

void MyClass::func(std::list<proiezione> tList)
{
    mem.swap(tList);
}

Or use the assign member:

mem.assign(tList.begin(), tList.end());

Or use the copy-assignment operator:

mem = tList;

Without seeing more code, it would be hard to tell you why it is empty.

Almost forgot (credit Casey with reminding me): or is actually a reserved word, so you'll want to name your variable something else.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • @volperossa What is the declaration and implementation of `calcola_proiezione`? – Zac Howland Dec 09 '13 at 19:44
  • it's not the problem. I try also this with same results: `//calcola_proiezione(ora,&pr, es, media_desiderata,valore_lode); std::list test; proiezione nuova; test.push_back(nuova); for (std::list::iterator it = test.begin(); it != test.end(); ++it) this->orb.push_back(*it);` results: orb.size()=0; test.size()=1 I think the problem is in declaration of orb; Should I initialize it to any value?? – volperossa Dec 09 '13 at 22:15
  • There is nothing wrong with the way you have initialized `orb` (at least none that is shown in the code you have provided). You do not need to initialize the list when you declare it because its default constructor does that. – Zac Howland Dec 09 '13 at 22:38