Problem with the converting the iterator

What is wrong with my 'for'-loop

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
27
28
29
30
31
32
33

#include<iostream>
#include<list>

using namespace std;

class zahl {
public: 
  int wert;
};

list<zahl> L;			// L ist eine Liste aus Ganzzahlen
typedef list<zahl>::iterator it;

int main(){

  zahl z1;
  z1.wert = 11;
  
  L.push_back(z1);
  
  z1.wert = 22;
  L.push_front(z1);
  
  z1.wert = 33;
  L.insert(++L.begin(), z1);

  for (it i=L.begin();i=L.end();i++){
    cout << i->wert << "\t";
  }
  cout << endl;

}


/*
sgi-list.cpp: In function ‘int main()’:
sgi-list.cpp:31:31: error: could not convert ‘(i = (*(const std::_List_iterator<zahl>*)(& L.std::list<_Tp, _Alloc>::end<zahl, std::allocator<zahl> >())))’ from ‘it {aka std::_List_iterator<zahl>}’ to ‘bool’
for (it i=L.begin();i=L.end();i++){

*/

any Hints
Thx in advance
ccslave
When you read the error message you should ask yourself why it is trying to convert the variable i to a bool.
could not convert foo to 'bool'

That is the hint. (The error message mentions 31:31, which probably means "line 31, column 31". Akward.)

Lets make it a bit easier to pinpoint the error by adding some whitespace:
1
2
3
4
5
6
7
8
for (
  it i=L.begin();
  i=L.end();
  i++ )
{
    cout << i->wert << "\t";
}
cout << endl;

If you try that, I bet that the error in on the equivalent of line 3, the condition that decides whether the loop continues to iterate. Which operator do you have there?


Edit:
C++11 has a range-based for-loop syntax and a new meaning for keyword auto:
1
2
3
for ( auto x : L ) {
  cout << x.wert << '\t';
}
Last edited on
1
2
3
4
5
6
7
8
9
10
.
.
for ( it i=L.begin();  
  i!=L.end();  
  ++i )
 {
  cout << i->wert << '\t';
}
.
.


from '=' to '!=' and that was the problem

thx
Topic archived. No new replies allowed.