<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Speicherzugriffsfehler]]></title><description><![CDATA[<p>Hallo,</p>
<p>habe folgendes Programm, welches ohne Fehlermeldung und warning einwandfrei kompiliert. Beim Aufruf kommt jedoch nur &quot;Speicherzugriffsfehler&quot;. Leider kenn ich mich in C++ noch zu wenig aus, deswegen finde ich den Fehler wahrscheinlich auch nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Code:</p>
<pre><code>#ifndef CITY_H_
#define CITY_H_

#include &lt;iostream&gt;
#include &lt;set&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;

class EOFException {};

class City
{
public:
  City (istream &amp;s)
  {
    s &gt;&gt; id &gt;&gt; name &gt;&gt; population &gt;&gt; region &gt;&gt; sealevel;
    if (!s)
	throw EOFException ();
  }

  City (int pId) { id = pId; }
  City (string pName) {name = pName; }

  void setMarker (int i) { marked = i; }

  int getMarker () { return marked; }

  int getId () { return id; }

  string getName() { return name; }

  void print (ostream &amp;o = cout) const
  {
    o &lt;&lt; id &lt;&lt; &quot; &quot; &lt;&lt; name &lt;&lt; endl;
  }

  bool operator&lt; (const City &amp;c) const  { return id &lt; c.id; }

private:
  unsigned int id;
  string name;
  unsigned int marked;
  unsigned int population;
  unsigned int region;
  unsigned int sealevel;
};

class CityList
{
public:
  CityList ();
  CityList (char *FileName);
  City const *findCityById (unsigned id) const
  { 
    City c (id); return &amp;*cities.find (c); 
  }
  void insertCity (City c) { cities.insert(c); }

  void setCities(set&lt;City&gt; &amp;cs) {cities = cs; }

  City *findCityById (int id) 
  {
  	set&lt;City&gt;::iterator it;
	for (it = cities.begin(); it != cities.end(); ++it)
	{
		City c = *it;
		City&amp; cn = c;
		if (c.getId() == id)
			cn = *it;
			return &amp;cn;
	}
	return 0;
  }

  City *findCityByName (string name) 
  {
  	set&lt;City&gt;::iterator it;
	for (it = cities.begin(); it != cities.end(); ++it)
	{
		City c = *it;
		City&amp; cn = c;
		if (c.getName() == name)
			cn = *it;
			return &amp;cn;
	}
	return 0;
  }

  void printList() 
  {
    set&lt;City&gt;::iterator it;
    for (it = cities.begin(); it != cities.end(); ++it)
	{
	  City c = *it;
	  City&amp; cn = c;
      cn.print();	
    }	  
  }

private:
  set&lt;City&gt; cities;
};

#endif
</code></pre>
<pre><code>#ifndef STREET_H
#define STREET_H

#include &quot;city.h&quot;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;set&gt;

using namespace std;

class Street
{
public:

  Street (istream &amp;s)
  { 
    s &gt;&gt; fromId &gt;&gt; toId &gt;&gt; distance;
    if (!s)  throw EOFException ();
  }

  int getFromId () { return fromId; }

  int getToId () { return toId; }

 private:
  unsigned int fromId;
  unsigned int toId;
  unsigned int distance;
};

class StreetList
{
public:
  StreetList (char *FileName, CityList &amp;cl);
  void findReachableCities (CityList &amp;cl, StreetList &amp;sl, string name);
  vector&lt;Street&gt; getStreets()
  {
  return streets;
  }

private:
 vector&lt;Street&gt; streets;
 CityList &amp;cities;
};

#endif
</code></pre>
<pre><code>#include &lt;fstream&gt;
#include &quot;city.h&quot;

using namespace std;

CityList::CityList (char *fileName)
{
  ifstream s (fileName);
  try  { for (;;) cities.insert (s); }
  catch (EOFException) {}
}
</code></pre>
<pre><code>#include &lt;fstream&gt;
#include &quot;street.h&quot;
#include &quot;city.h&quot;
#include &lt;set&gt;

using namespace std;

StreetList::StreetList (char *fileName, CityList &amp;cl): cities (cl) 
{
  ifstream s (fileName);
  try  
  { 
  for (;;)  streets.push_back (s);
  }
  catch (EOFException) {}
}

void StreetList::findReachableCities (CityList &amp;cl, StreetList &amp;sl, string name)
{
  unsigned a = 0;
  set&lt;City&gt; reachableCities;
  City *startCity = cl.findCityByName(name);
  reachableCities.insert(*startCity);
  startCity-&gt;setMarker(1);
  cl.findCityByName(name)-&gt;setMarker(1);
  set&lt;City&gt;::iterator it = reachableCities.begin();
  do
  {
	for(unsigned i = 0; i &lt; sl.getStreets().size(); i++)
	{
	  advance(it, a);
	  City c = *it;
	  if ((c.getId()) == (sl.getStreets()[i].getFromId()) &amp;&amp; \
	  ((cl.findCityById(sl.getStreets()[i].getToId()))-&gt;getMarker() == 0))
	  {   
		City *neighborCity = cl.findCityById(sl.getStreets()[i].getToId());
		City&amp; cn = *neighborCity;
		reachableCities.insert(cn);
		neighborCity-&gt;setMarker(1);
		cl.findCityById(sl.getStreets()[i].getToId())-&gt;setMarker(1);
	  }
	}
	a++;
  }
  while(a &lt; reachableCities.size());
  cl.setCities(reachableCities);
}
</code></pre>
<pre><code>#include &quot;city.h&quot;
#include &quot;street.h&quot;
#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

int main(int argc, char *argv[]) 
{
  if (argc &lt; 5) {
    cerr &lt;&lt; &quot;usage: programm stadt.dat gebiet.dat strasse.dat stadtname&quot; &lt;&lt; endl;
    exit(1);
  }

  CityList cl (argv[1]);
  StreetList sl(argv[3], cl);

  sl.findReachableCities(cl, sl, argv[4]);
  cl.printList();
}
</code></pre>
<p>Ich hoffe, ihr könnt mir helfen!</p>
<p>elflaco</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/330868/speicherzugriffsfehler</link><generator>RSS for Node</generator><lastBuildDate>Thu, 02 Jul 2026 15:54:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/330868.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Jan 2015 17:06:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:35:31 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>habe folgendes Programm, welches ohne Fehlermeldung und warning einwandfrei kompiliert. Beim Aufruf kommt jedoch nur &quot;Speicherzugriffsfehler&quot;. Leider kenn ich mich in C++ noch zu wenig aus, deswegen finde ich den Fehler wahrscheinlich auch nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Code:</p>
<pre><code>#ifndef CITY_H_
#define CITY_H_

#include &lt;iostream&gt;
#include &lt;set&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;

class EOFException {};

class City
{
public:
  City (istream &amp;s)
  {
    s &gt;&gt; id &gt;&gt; name &gt;&gt; population &gt;&gt; region &gt;&gt; sealevel;
    if (!s)
	throw EOFException ();
  }

  City (int pId) { id = pId; }
  City (string pName) {name = pName; }

  void setMarker (int i) { marked = i; }

  int getMarker () { return marked; }

  int getId () { return id; }

  string getName() { return name; }

  void print (ostream &amp;o = cout) const
  {
    o &lt;&lt; id &lt;&lt; &quot; &quot; &lt;&lt; name &lt;&lt; endl;
  }

  bool operator&lt; (const City &amp;c) const  { return id &lt; c.id; }

private:
  unsigned int id;
  string name;
  unsigned int marked;
  unsigned int population;
  unsigned int region;
  unsigned int sealevel;
};

class CityList
{
public:
  CityList ();
  CityList (char *FileName);
  City const *findCityById (unsigned id) const
  { 
    City c (id); return &amp;*cities.find (c); 
  }
  void insertCity (City c) { cities.insert(c); }

  void setCities(set&lt;City&gt; &amp;cs) {cities = cs; }

  City *findCityById (int id) 
  {
  	set&lt;City&gt;::iterator it;
	for (it = cities.begin(); it != cities.end(); ++it)
	{
		City c = *it;
		City&amp; cn = c;
		if (c.getId() == id)
			cn = *it;
			return &amp;cn;
	}
	return 0;
  }

  City *findCityByName (string name) 
  {
  	set&lt;City&gt;::iterator it;
	for (it = cities.begin(); it != cities.end(); ++it)
	{
		City c = *it;
		City&amp; cn = c;
		if (c.getName() == name)
			cn = *it;
			return &amp;cn;
	}
	return 0;
  }

  void printList() 
  {
    set&lt;City&gt;::iterator it;
    for (it = cities.begin(); it != cities.end(); ++it)
	{
	  City c = *it;
	  City&amp; cn = c;
      cn.print();	
    }	  
  }

private:
  set&lt;City&gt; cities;
};

#endif
</code></pre>
<pre><code>#ifndef STREET_H
#define STREET_H

#include &quot;city.h&quot;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;set&gt;

using namespace std;

class Street
{
public:

  Street (istream &amp;s)
  { 
    s &gt;&gt; fromId &gt;&gt; toId &gt;&gt; distance;
    if (!s)  throw EOFException ();
  }

  int getFromId () { return fromId; }

  int getToId () { return toId; }

 private:
  unsigned int fromId;
  unsigned int toId;
  unsigned int distance;
};

class StreetList
{
public:
  StreetList (char *FileName, CityList &amp;cl);
  void findReachableCities (CityList &amp;cl, StreetList &amp;sl, string name);
  vector&lt;Street&gt; getStreets()
  {
  return streets;
  }

private:
 vector&lt;Street&gt; streets;
 CityList &amp;cities;
};

#endif
</code></pre>
<pre><code>#include &lt;fstream&gt;
#include &quot;city.h&quot;

using namespace std;

CityList::CityList (char *fileName)
{
  ifstream s (fileName);
  try  { for (;;) cities.insert (s); }
  catch (EOFException) {}
}
</code></pre>
<pre><code>#include &lt;fstream&gt;
#include &quot;street.h&quot;
#include &quot;city.h&quot;
#include &lt;set&gt;

using namespace std;

StreetList::StreetList (char *fileName, CityList &amp;cl): cities (cl) 
{
  ifstream s (fileName);
  try  
  { 
  for (;;)  streets.push_back (s);
  }
  catch (EOFException) {}
}

void StreetList::findReachableCities (CityList &amp;cl, StreetList &amp;sl, string name)
{
  unsigned a = 0;
  set&lt;City&gt; reachableCities;
  City *startCity = cl.findCityByName(name);
  reachableCities.insert(*startCity);
  startCity-&gt;setMarker(1);
  cl.findCityByName(name)-&gt;setMarker(1);
  set&lt;City&gt;::iterator it = reachableCities.begin();
  do
  {
	for(unsigned i = 0; i &lt; sl.getStreets().size(); i++)
	{
	  advance(it, a);
	  City c = *it;
	  if ((c.getId()) == (sl.getStreets()[i].getFromId()) &amp;&amp; \
	  ((cl.findCityById(sl.getStreets()[i].getToId()))-&gt;getMarker() == 0))
	  {   
		City *neighborCity = cl.findCityById(sl.getStreets()[i].getToId());
		City&amp; cn = *neighborCity;
		reachableCities.insert(cn);
		neighborCity-&gt;setMarker(1);
		cl.findCityById(sl.getStreets()[i].getToId())-&gt;setMarker(1);
	  }
	}
	a++;
  }
  while(a &lt; reachableCities.size());
  cl.setCities(reachableCities);
}
</code></pre>
<pre><code>#include &quot;city.h&quot;
#include &quot;street.h&quot;
#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

int main(int argc, char *argv[]) 
{
  if (argc &lt; 5) {
    cerr &lt;&lt; &quot;usage: programm stadt.dat gebiet.dat strasse.dat stadtname&quot; &lt;&lt; endl;
    exit(1);
  }

  CityList cl (argv[1]);
  StreetList sl(argv[3], cl);

  sl.findReachableCities(cl, sl, argv[4]);
  cl.printList();
}
</code></pre>
<p>Ich hoffe, ihr könnt mir helfen!</p>
<p>elflaco</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440397</guid><dc:creator><![CDATA[elflaco]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:35:31 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:09:12 GMT]]></title><description><![CDATA[<p>Ich bin sogar ziemlich sicher, dass der Compiler bei</p>
<pre><code class="language-cpp">City *findCityById (int id)
  {
    set&lt;City&gt;::iterator it;
    for (it = cities.begin(); it != cities.end(); ++it)
    {
        City c = *it;
        City&amp; cn = c;
        if (c.getId() == id)
            cn = *it;
            return &amp;cn;
    }
    return 0;
  }
</code></pre>
<p>eine Warnung ausgibt, wenn er richtig eingestellt ist (z.B. -Wall -Wextra bei gcc).</p>
<p>Tipp: Du gibst einen Pointer auf eine lokale Variable zurück.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440398</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440398</guid><dc:creator><![CDATA[würnung]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:09:12 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:10:49 GMT]]></title><description><![CDATA[<p>elflaco schrieb:</p>
<blockquote>
<pre><code>#include &quot;city.h&quot;
#include &quot;street.h&quot;
#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

int main(int argc, char *argv[]) 
{
  if (argc &lt; 4) {
    cerr &lt;&lt; &quot;usage: programm stadt.dat gebiet.dat strasse.dat stadtname&quot; &lt;&lt; endl;
    exit(1);
  }

  CityList cl (argv[1]);
  StreetList sl(argv[3], cl);

  sl.findReachableCities(cl, sl, argv[4]);
  cl.printList();
}
</code></pre>
<p>Ich hoffe, ihr könnt mir helfen!</p>
<p>elflaco</p>
</blockquote>
<p>Beim schnellen überfliegen fällt mir auf, dass Du auf argc &lt; 4 abfragst. Bei argc==4 darfst Du aber nicht auf argv[4] zugreifen sondern nur bis argv[3].</p>
<p>Und lass das &quot;using namespace std&quot; weg. Zumindest im Header. Du musst dann nur die Symbole wie cout voll qualifizieren (std::cout ...).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440399</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:10:49 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:13:24 GMT]]></title><description><![CDATA[<p>würnung schrieb:</p>
<blockquote>
<p>Ich bin sogar ziemlich sicher, dass der Compiler bei</p>
<pre><code class="language-cpp">City *findCityById (int id)
  {
    set&lt;City&gt;::iterator it;
    for (it = cities.begin(); it != cities.end(); ++it)
    {
        City c = *it;
        City&amp; cn = c;
        if (c.getId() == id)
            cn = *it;
            return &amp;cn;
    }
    return 0;
  }
</code></pre>
<p>eine Warnung ausgibt, wenn er richtig eingestellt ist (z.B. -Wall -Wextra bei gcc).</p>
<p>Tipp: Du gibst einen Pointer auf eine lokale Variable zurück.</p>
</blockquote>
<p>Nein, der Compiler gibt keine Warnung aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Hier mein Makefile:</p>
<pre><code>CXXFLAGS=-Wall
CC=$(CXX)

PROGRAMS=programm
OBJECTS=city.o street.o

ALLOBJECTS=$(OBJECTS) $(patsubst %,%.o,$(PROGRAMS))

default: $(PROGRAMS)

%.d: %.cc
	$(SHELL) -ec '$(CC) -MM $(CXXFLAGS) $&lt; | \
	perl -pe '&quot;'&quot;'s|($*\.o)[ :]*|\1 $@: |g'&quot;'&quot;' &gt; $@'

programm: $(OBJECTS)

clean:
	-rm -f $(ALLOBJECTS) $(ALLOBJECTS:%.o=%.d) $(PROGRAMS)

include $(ALLOBJECTS:%.o=%.d)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2440400</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440400</guid><dc:creator><![CDATA[elflaco]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:13:24 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:14:10 GMT]]></title><description><![CDATA[<p>tntnet schrieb:</p>
<blockquote>
<p>Beim schnellen überfliegen fällt mir auf, dass Du auf argc &lt; 4 abfragst. Bei argc==4 darfst Du aber nicht auf argv[4] zugreifen sondern nur bis argv[3].</p>
</blockquote>
<p>Er darf, aber bei argv[4] sitzt ein Null-Zeiger.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440402</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440402</guid><dc:creator><![CDATA[language lawyer]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:14:10 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:17:47 GMT]]></title><description><![CDATA[<p>tntnet schrieb:</p>
<blockquote>
<p>elflaco schrieb:</p>
<blockquote>
<pre><code>#include &quot;city.h&quot;
#include &quot;street.h&quot;
#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

int main(int argc, char *argv[]) 
{
  if (argc &lt; 4) {
    cerr &lt;&lt; &quot;usage: programm stadt.dat gebiet.dat strasse.dat stadtname&quot; &lt;&lt; endl;
    exit(1);
  }

  CityList cl (argv[1]);
  StreetList sl(argv[3], cl);

  sl.findReachableCities(cl, sl, argv[4]);
  cl.printList();
}
</code></pre>
<p>Ich hoffe, ihr könnt mir helfen!</p>
<p>elflaco</p>
</blockquote>
<p>Beim schnellen überfliegen fällt mir auf, dass Du auf argc &lt; 4 abfragst. Bei argc==4 darfst Du aber nicht auf argv[4] zugreifen sondern nur bis argv[3].</p>
<p>Und lass das &quot;using namespace std&quot; weg. Zumindest im Header. Du musst dann nur die Symbole wie cout voll qualifizieren (std::cout ...).</p>
</blockquote>
<p>Oh, kleiner Fehler, soll natürlich argc &lt; 5 heißen!</p>
<p>Hmm, ist das schlimm, wenn ich das solasse?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440403</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440403</guid><dc:creator><![CDATA[elflaco]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:17:47 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:19:27 GMT]]></title><description><![CDATA[<p>elflaco schrieb:</p>
<blockquote>
<p>Nein, der Compiler gibt keine Warnung aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Hier mein Makefile:</p>
</blockquote>
<p>Stimmt. Nur clang gibt eine Warnung aus.</p>
<pre><code>xx.cc:19:15: warning: address of stack memory associated with local variable 'c'
      returned [-Wreturn-stack-address]
      return &amp;cn;
              ^~
xx.cc:16:13: note: binding reference variable 'cn' here
      City&amp; cn = c;
            ^    ~
1 warning generated.
</code></pre>
<p>Solltest du auf jeden Fall fixen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440404</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440404</guid><dc:creator><![CDATA[würnung]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:19:27 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:35:07 GMT]]></title><description><![CDATA[<p>würnung schrieb:</p>
<blockquote>
<p>elflaco schrieb:</p>
<blockquote>
<p>Nein, der Compiler gibt keine Warnung aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Hier mein Makefile:</p>
</blockquote>
<p>Stimmt. Nur clang gibt eine Warnung aus.</p>
<pre><code>xx.cc:19:15: warning: address of stack memory associated with local variable 'c'
      returned [-Wreturn-stack-address]
      return &amp;cn;
              ^~
xx.cc:16:13: note: binding reference variable 'cn' here
      City&amp; cn = c;
            ^    ~
1 warning generated.
</code></pre>
<p>Solltest du auf jeden Fall fixen!</p>
</blockquote>
<p>Ok, danke schonmal!<br />
Aber wie am besten? Hab schon was ausprobiert, aber das hat alles nicht zum Erfolg geführt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440407</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440407</guid><dc:creator><![CDATA[elflaco]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:35:07 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:48:34 GMT]]></title><description><![CDATA[<pre><code>City *findCityById (int id) 
  {
  	set&lt;City&gt;::iterator it;
	for (it = cities.begin(); it != cities.end(); ++it)
	{
		City&amp; cn = *it;
		if (cn.getId() == id)
			return &amp;cn;
	}
	return 0;
  }
</code></pre>
<p>So zum Beispiel. Wenn nicht wieder so ein Besserwisser kommt. Das ist nicht so sauber aber für einen Anfänger hinreichend korrekt.</p>
<p>Mit Deinem Code erzeugst Du eine Kopie von *it mit</p>
<pre><code>City c = *it;
        City&amp; cn = c;
        if (c.getId() == id)
            cn = *it;
            return &amp;cn;
</code></pre>
<p><code>c</code> ist hier eine Kopie von *it und damit eine lokale Variable und cn eine Referenz darauf. Ein Zeiger auf cn zeigt also auf die Lokale Kopie <code>c</code></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440411</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:48:34 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 17:55:04 GMT]]></title><description><![CDATA[<p>tntnet schrieb:</p>
<blockquote>
<pre><code>City *findCityById (int id) 
  {
  	set&lt;City&gt;::iterator it;
	for (it = cities.begin(); it != cities.end(); ++it)
	{
		City&amp; cn = *it;
		if (cn.getId() == id)
			return &amp;cn;
	}
	return 0;
  }
</code></pre>
<p>So zum Beispiel. Wenn nicht wieder so ein Besserwisser kommt. Das ist nicht so sauber aber für einen Anfänger hinreichend korrekt.</p>
<p>Mit Deinem Code erzeugst Du eine Kopie von *it mit</p>
<pre><code>City c = *it;
        City&amp; cn = c;
        if (c.getId() == id)
            cn = *it;
            return &amp;cn;
</code></pre>
<p><code>c</code> ist hier eine Kopie von *it und damit eine lokale Variable und cn eine Referenz darauf. Ein Zeiger auf cn zeigt also auf die Lokale Kopie <code>c</code></p>
</blockquote>
<p>So ähnlich hatte ich es auch schon, dann kommt aber:</p>
<pre><code>city.h:72:21: error: invalid initialization of reference of type ‘City&amp;’ from expression of type ‘const City’
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2440414</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440414</guid><dc:creator><![CDATA[elflaco]]></dc:creator><pubDate>Thu, 29 Jan 2015 17:55:04 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 18:39:01 GMT]]></title><description><![CDATA[<p>Ein set mag es nicht so gerne, wenn man seine Elemente einfach so ändert <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440418</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440418</guid><dc:creator><![CDATA[Jockelx]]></dc:creator><pubDate>Thu, 29 Jan 2015 18:39:01 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 18:45:20 GMT]]></title><description><![CDATA[<p>Jockelx schrieb:</p>
<blockquote>
<p>Ein set mag es nicht so gerne, wenn man seine Elemente einfach so ändert <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Das habe ich auch schon bemerkt...aber wie machs ich dann sonst am besten? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440419</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440419</guid><dc:creator><![CDATA[elflaco]]></dc:creator><pubDate>Thu, 29 Jan 2015 18:45:20 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Thu, 29 Jan 2015 18:57:18 GMT]]></title><description><![CDATA[<p>Ich weiss nicht, was dein Programm machen soll.<br />
Brauchst du wirklich ein set? Brauchst du wirklich eine Referenz auf das Orginal oder kannst du es auch genauso gut kopieren?<br />
Falls beides mit 'ja' beantwortet wird, überleg dir eine Alternative.<br />
Ein Element 'ändern' in einem set kannst du z.B. mit löschen und einfügen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440421</guid><dc:creator><![CDATA[Jockelx]]></dc:creator><pubDate>Thu, 29 Jan 2015 18:57:18 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Fri, 30 Jan 2015 07:29:15 GMT]]></title><description><![CDATA[<p>Jockelx schrieb:</p>
<blockquote>
<p>Ein set mag es nicht so gerne, wenn man seine Elemente einfach so ändert <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Ach ja richtig. Ich habe gar nicht daran gedacht, dass man die Elemente im set const sind.</p>
<p>Das Problem, wenn das set erlauben würde, die Elemente zu ändern ist, dass die Reihenfolge kaputt gehen könnte und eine binäre Suche dann nicht mehr funktioniert.</p>
<p>Also musst Du entweder auf ein std::vector oder ähnliches ausweichen oder doch gleich richtig und const-correctness lernen. Ändere mal den Rückgabewert von Deiner &quot;findCityBy...&quot;-Methoden in &quot;const City*&quot;. Die get-Methoden in City machst Du auch const, so dass Du auf diese über einen const* zugreifen kannst. Es gibt sicher noch ein paar andere stellen, wo Du const ergänzen musst. Das wird Dir dein Compiler sogar sagen. Die Parameter von &quot;findReachableCities&quot; fallen mir gerade noch ins Auge.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440460</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440460</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Fri, 30 Jan 2015 07:29:15 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherzugriffsfehler on Sat, 31 Jan 2015 11:46:57 GMT]]></title><description><![CDATA[<p>Wenn ich das richtig intepretiere: Du hast Städte (Knoten) und Straßen (Kanten), die diese Knoten verbinden. Ausgehend von einem Startknoten willst Du alle erreichbaren Knoten finden?</p>
<p>Ist das für die Uni oder privat?<br />
Scheint mir auf jeden Fall eine dankbare Aufgabe für's Forum zu sein, da man viel daran zeigen kann...<br />
SW-Design, Standardbibliothek, Graphentheorie.</p>
<p>...natürlich nur wenn der OP noch am Ball ist und nicht am Freitag Abgabe war und die Sache damit abgehakt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440603</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Sat, 31 Jan 2015 11:46:57 GMT</pubDate></item></channel></rss>