For loop does not modify values of vector

Hi! Hope you are having a great day.
Is life not beautiful?
Anyway...
I came across this for loop that uses this value:

1
2
private:
    vector<Flug*> Fluege; //Fluege is a private member of a class. 

Here is the for loop:
1
2
3
4
5
6
7
8
9
10
for(unsigned int i{1};i<Fluege.size();i++)
    {
        cout<<endl<<endl;
        cout<<"i= "<<i<<endl;
        cout<<Fluege.at(i)->print_flugdata()<<endl;
        string name=intdatei_tostring(i);
        *Fluege.at(i)=FlugeinerDateilesen(name,*Fluege.at(i));//assign this //data to the vector at position i
        cout<<Fluege.at(i)->print_flugdata()<<endl;
    cout<<endl<<endl;
    }


Obviously, the value does NOT get modified and always when I try to change it, I get this compiled:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Here is the first iteration. It goes all good, at first the vector is empty.
i= 1
0   0
Name: hierspeichern_2.txt
6 ki ko 718 //and this confirms the vector at position i was successfully filled up

//here is the second iteration
Iteration through Fluegesize begins. 
i= 2
6 ki ko 718 //here, although not modified, the value of Fluege at position i=2
//was already changed before assigning the information...
//This means, for some reason the changes done to one iterator were done to all
Name: hierspeichern_3.txt
5 lll ppp 7714//and this confirms the vector at position i was successfully filled up


//see here, although Flug 2 should have the values from before:
//6 ki ko 718
//it has the values of the last iteration with i=3!
Flug 2:
Flug Info:
Anzahlplaetze: 5.Abflug: lll. Ziel: ppp. Flugnummer: 7714.

Flug 3:
Flug Info:
Anzahlplaetze: 5.Abflug: lll. Ziel: ppp. Flugnummer: 7714.


I hope you help me because I have not been able to understand what am I doing wrong...

Thanks in advance!
Last edited on
How is the vector Fluege initialized?
First, the vector contains pointers. Those values "in the vector" are not modified. The values pointed to by the pointers you do try to modify.

Second, if this is the modifying code:
1
2
string name = intdatei_tostring( i );
*Fluege.at(i) = FlugeinerDateilesen( name, *Fluege.at(i) );

Then are you sure that the return value of FlugeinerDateilesen( name, x ) does not equal x?

Finally,
1
2
3
for ( unsigned int i{1}; i<Fluege.size(); i++ ) {
  // ...
}

The valid indices of std::vector start from 0. This loop starts from 1. It skips the first element of the vector.
Last edited on
Hello. coder 777, the vector Fluege is, translated to english, "flights". (flights == Fluege).
The class where the vector Flights or Fluege is, is called "offer".

Keskiverto, yes, I want to try to change the values "pointed to" by the pointers.


Then are you sure that the return value of FlugeinerDateilesen( name, x ) does not equal x?


Yes, I am sure because FlugeinerDateilesen means Readflightofafile. It gets as parameters a name, which changes according to the number of i (example flight1, flight2, flight 3 and so on). And it searches those files and reads the data from there, to put it in the vector.

I know the first element of the vector is skipped. This is done purposefully, to read the rest of elements, if there is more than one value in the vector.

Does someone know how to keep those "flights" modified? Should I add other methods here? I thought I just needed to add some ampersand somewhere and this would compile easily
Alright guys! It was my mistake here...
The vector of "Fluege" (or "Flights") was actually defined by me as a vector of POINTERS of flights...

Yes... My mistake...

Then I changed it to be an actual vector of flights.

And now it works good!

Thanks so much for the people who were willing to help. My code compiles good now.

Greetings!
Topic archived. No new replies allowed.