<?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[new problem]]></title><description><![CDATA[<p>Hallo ich hab da ein Problem mit dem Code. Kann mit wer helfen</p>
<pre><code>void Universum::AddStar(String &amp;s,double x, double y, double z)
{
    Stern **h = new Stern*[anzahl_sterne+1];
    for(int i = 0; i &lt; anzahl_sterne; i++)
    {
//        h[i] = new Stern(stars[i].GetName(),stars[i].GetX(),stars[i].GetY(),stars[i].GetZ());
        delete this-&gt;stars[i];
    }
    h[anzahl_sterne] = new Stern(s,x,y,z);
    delete [] stars;
    stars = h;
    anzahl_sterne++;
}
</code></pre>
<p>Die Zeile die kommentiert ist wird immer als fehler erkannt.</p>
<p>die Variable stars ist vom Datentyp: Stars **</p>
<p>Der Konstruktor von Stern sieht folgendermasen aus:</p>
<pre><code>Stern::Stern(String &amp;name,double x,double y,double z)
{
    this-&gt;name = name;
    this-&gt;x = x;
    this-&gt;y = y;
    this-&gt;z = z;
}
</code></pre>
<p>Danke für die hilfe im Voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/253349/new-problem</link><generator>RSS for Node</generator><lastBuildDate>Sun, 13 Sep 2026 21:44:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/253349.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 01 Nov 2009 09:34:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to new problem on Sun, 01 Nov 2009 09:34:02 GMT]]></title><description><![CDATA[<p>Hallo ich hab da ein Problem mit dem Code. Kann mit wer helfen</p>
<pre><code>void Universum::AddStar(String &amp;s,double x, double y, double z)
{
    Stern **h = new Stern*[anzahl_sterne+1];
    for(int i = 0; i &lt; anzahl_sterne; i++)
    {
//        h[i] = new Stern(stars[i].GetName(),stars[i].GetX(),stars[i].GetY(),stars[i].GetZ());
        delete this-&gt;stars[i];
    }
    h[anzahl_sterne] = new Stern(s,x,y,z);
    delete [] stars;
    stars = h;
    anzahl_sterne++;
}
</code></pre>
<p>Die Zeile die kommentiert ist wird immer als fehler erkannt.</p>
<p>die Variable stars ist vom Datentyp: Stars **</p>
<p>Der Konstruktor von Stern sieht folgendermasen aus:</p>
<pre><code>Stern::Stern(String &amp;name,double x,double y,double z)
{
    this-&gt;name = name;
    this-&gt;x = x;
    this-&gt;y = y;
    this-&gt;z = z;
}
</code></pre>
<p>Danke für die hilfe im Voraus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1801529</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1801529</guid><dc:creator><![CDATA[glantschnig]]></dc:creator><pubDate>Sun, 01 Nov 2009 09:34:02 GMT</pubDate></item><item><title><![CDATA[Reply to new problem on Sun, 01 Nov 2009 10:24:07 GMT]]></title><description><![CDATA[<p>Du verrätst relativ wenig von Deinem Code und auch nicht die Fehlermeldung des Compilers!</p>
<p>Was auf jeden Fall falsch ist, ist <code>stars[i].XXX</code> , weil <code>stars[i]</code> nicht ein Star-Objekt selbst, sondern nur ein Zeiger darauf ist. Es muss also <code>stars[i]-&gt;XXX</code> heißen.</p>
<p>Was sonst noch schlecht ist:<br />
- Du willst ein Star-Objekt kopieren. Für so etwas gibt es es Kopierkonstruktor, die Du nutzen solltest<br />
- Du machst es Dir mit <code>Star**</code> unnötig kompliziert und fehleranfällig.</p>
<p>Probier mal diesen Ansatz:</p>
<pre><code class="language-cpp">struct Star
{
  std::string name;
  double x, y, z;
  // normales &quot;Aggregat&quot; (keine benutzerdefinierten Konstruktoren)
};

class Universum
{
public:
  void addStar(Star const&amp; x) {stars_.push_back(x);}
  int numStars() const {return stars_.size();}
private:
  std::vector&lt;Star&gt; stars_;
};

int main()
{
  Universum u;
  Star s = {&quot;Dings&quot;, 1.0, 3.0, 2.0}; // Aggregat-Initialisierung
  u.addStar(s);
}
</code></pre>
<p>Beachte: Hier wurden keine Zeiger verwendet und kein new/delete. Die dynamische Speicherverwaltung wird in std::vector <a href="http://de.wikipedia.org/wiki/Datenkapselung_(Programmierung)" rel="nofollow">gekapselt</a>. Kapselung ist etwas feines!</p>
<p>Mach Dir auch klar, was das <code>const</code> jeweils im Quellcode zu suchen hat, wenn Du es noch nicht weißt.</p>
<p>Du kannst Star natürlich auch so schreiben:</p>
<pre><code class="language-cpp">struct Star
{
  std::string name;
  double x, y, z;

  Star() : x(0), y(0), z(0) {}
  explicit Star(std::string const&amp; n,
    double x, double y, double z)
  : name(n), x(x), y(y), z(z) {}
  // kein Aggregat mehr. Die Klasse bekommt vom
  // Compiler dennoch einen impliziten Kopierkonstruktor
  // und Zuweisungsoperator =
};
</code></pre>
<p>und dann so benutzen</p>
<pre><code class="language-cpp">int main()
{
  std::vector&lt;Star&gt; sterne;
  sterne.push_back( Star(&quot;Dings&quot;,1.0,3.0,2.0) );
  Star dings = sterne[0]; // &quot;Kopier-Initialisierung&quot; (copy-ctor)
  Star bums  = dings;     // &quot;Kopier-Initialisierung&quot; (copy-ctor)
  dings = bums;           // &quot;Kopier-Zuweisung&quot; (operator=)
}
</code></pre>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1801538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1801538</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sun, 01 Nov 2009 10:24:07 GMT</pubDate></item><item><title><![CDATA[Reply to new problem on Sun, 01 Nov 2009 11:16:27 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">explicit Star(std::string const&amp; n, 
    double x, double y, double z) 
  : name(n), x(x), y(y), z(z) {}
</code></pre>
<p>explicit ist doch hier gar nicht nötig!?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1801554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1801554</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 01 Nov 2009 11:16:27 GMT</pubDate></item><item><title><![CDATA[Reply to new problem on Sun, 01 Nov 2009 12:00:53 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<pre><code class="language-cpp">explicit Star(std::string const&amp; n, 
    double x, double y, double z) 
  : name(n), x(x), y(y), z(z) {}
</code></pre>
<p>explicit ist doch hier gar nicht nötig!?</p>
<p>bb</p>
</blockquote>
<p>Korrket, denn das ist nur bei Konstruktoren mit einem Argument nötig.<br />
(Od. die Anzahl Argumente könnnen durch Default Werte zu einem Argument reduziert werden.)<br />
Simon</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1801583</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1801583</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Sun, 01 Nov 2009 12:00:53 GMT</pubDate></item><item><title><![CDATA[Reply to new problem on Sun, 01 Nov 2009 13:21:29 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<pre><code class="language-cpp">explicit Star(std::string const&amp; n, 
    double x, double y, double z) 
  : name(n), x(x), y(y), z(z) {}
</code></pre>
<p>explicit ist doch hier gar nicht nötig!?</p>
</blockquote>
<p>Hast recht. Das kommt davon, wenn man erst x,y,z, &quot;mit 0 defaulten&quot; will und sich dann umentschließt. <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>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1801624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1801624</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Sun, 01 Nov 2009 13:21:29 GMT</pubDate></item></channel></rss>