Problem with 'list'-iterator in function

Why did I get a segmentation fault?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


struct platz {
  int id;
  string name;
};

list<platz> P;			// P ist die globale Variable für die Orte

typedef list<platz>::iterator p_it;



typedef pair<int,int> knoten_paar;  // Verbindungslinie, Kante

list<knoten_paar> K;

typedef list<knoten_paar>::iterator k_it;

int def_platz();

int def_paare();

int such_platz(string, p_it);

// ----------------------------------------------------------------------
int main(){

  def_platz();
  def_paare();  
  
  p_it platz_id;
  
  cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  
  such_platz("Freiburg",platz_id);
  cout << "main: " << platz_id->id << "\t" << platz_id->name << endl;

  
}
// ----------------------------------------------------------------------------------------
int such_platz(string ein, p_it ort){
  cout << "ein:\t" << ein << endl;
  int ret=1;
  int z=0;
  p_it treffer;
  
  ort=P.begin();
  
  while (ort->name!=ein) {
    ort++;
  }

  cout << ort->id << "\t" << ort->name << endl;
   
  return ret;
}


All I want is a routine to tell me: at which point of my 'list' is Freiburg,
the problem I want to use the answer in 'main'!

thx in advance
ccslave
Last edited on
Line 32: platz_id is uninitialized.

Line 36, you pass the uninitialized pointer to such_platz(), but that doesn't matter because you assign ort a value at line 48.

Line 37: platz_id is still uninitialized. You passed it by value to such_platz(), so the caller's value is not updated. If you expect such_platz to update the caller's value, then pass it by reference.
And how to initialize a reference?


p_it& platz_id;


BTW: p_it& platz_id = P.begin();

do not work :(

tia ccslave
Changing line 32 as shown won't make any difference since you're passing it by value to such_platz(). If I understand what you want to do correctly,you want to pass the iterator by reference.

1
2
3
4
5
  p_it platz_id;  // This remains as it is.
...
int such_platz (string, p_it & iter);  // pass iterator by reference
...
int such_platz(string ein, p_it & ort)  // pass iterator by reference 

Now, when you update ort in such_platz(),you're updating platz_id in the caller.





*Ah*
finally I got the right place,
thx kjr
Topic archived. No new replies allowed.