<?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[In Templatekonstruktor Objekte erstellen]]></title><description><![CDATA[<p>Noch eine Andere Frage: Ich möchte hier im Konstruktur eine mit n angegebene Anzahl an Objekten in meinen vector pushen. Der Typ von T ist jedoch natürlich zu diesem Zeitpunkt unbekannt. Wie kann man das Problem umgehen?</p>
<pre><code class="language-cpp">template&lt;class T&gt; class MyTemplate{
  public:
    MyTemplate(int n);

  private:
    std::vector&lt;T*&gt; objs;
};

MyTemplate&lt;class T&gt;::MyTemplate(int n)
{
  while(--n)
    objs.push_back(new T());  //Fehler...
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/120436/in-templatekonstruktor-objekte-erstellen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 22 Aug 2026 07:42:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/120436.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 13 Sep 2005 11:01:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:01:19 GMT]]></title><description><![CDATA[<p>Noch eine Andere Frage: Ich möchte hier im Konstruktur eine mit n angegebene Anzahl an Objekten in meinen vector pushen. Der Typ von T ist jedoch natürlich zu diesem Zeitpunkt unbekannt. Wie kann man das Problem umgehen?</p>
<pre><code class="language-cpp">template&lt;class T&gt; class MyTemplate{
  public:
    MyTemplate(int n);

  private:
    std::vector&lt;T*&gt; objs;
};

MyTemplate&lt;class T&gt;::MyTemplate(int n)
{
  while(--n)
    objs.push_back(new T());  //Fehler...
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/870979</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870979</guid><dc:creator><![CDATA[__gast]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:01:19 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:14:03 GMT]]></title><description><![CDATA[<p>Das ist so auf den ersten Blick erstmal garkein Fehler oder Problem. Du stellst aber eine Anforderung an den Typ T, und zwar dass er einen Default-Konstruktor besitzt.</p>
<p>while(--n)<br />
wird übrigens schön schiefgehen, wenn n &lt;= 0 ist.</p>
<p>Edit:<br />
Du musst vor die Konstruktordefinition aber nochmal template &lt;class T&gt; schreiben und aus dem &lt;class T&gt; nur ein &lt;T&gt; machen.</p>
<p>Deutsch ist schon ne ziemlich schwere Sprache <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> (Anzahl meiner Edits...)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870987</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870987</guid><dc:creator><![CDATA[7H3 N4C3R]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:14:03 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:24:29 GMT]]></title><description><![CDATA[<p>Erstmal vielen Dank, aber ich habe das dumpfe gefühl, dass ich nicht raffe, was du meinst. Könntest du ein kleines Beispiel zeigen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870994</guid><dc:creator><![CDATA[__gast]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:24:29 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:26:17 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template &lt;class T&gt;ShufflePlayer&lt;T&gt;::ShufflePlayer(int nsongs)
</code></pre>
<p>*erleuchtung* thx</p>
]]></description><link>https://www.c-plusplus.net/forum/post/870996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/870996</guid><dc:creator><![CDATA[__gast]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:26:17 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:35:10 GMT]]></title><description><![CDATA[<p>Zu früh gefreut: Wenn ich jedoch</p>
<pre><code class="language-cpp">objs.push_back(new T(2,3)));
</code></pre>
<p>schreibe, da der Konstruktor meiner an das Template übergebenen Klasse <strong>MyClass</strong> zwei Parameter benötigt, bekomme ich wieder Fehlermeldungen!</p>
<pre><code>[C++ Error] main.cpp(48): E2034 Cannot convert 'int' to 'MyClass *'
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/871002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/871002</guid><dc:creator><![CDATA[__gast]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:35:10 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:39:16 GMT]]></title><description><![CDATA[<p>Mir scheint es, als wenn dein Templateparameter MyClass*, und nicht MyClass ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/871004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/871004</guid><dc:creator><![CDATA[7H3 N4C3R]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:39:16 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:51:48 GMT]]></title><description><![CDATA[<blockquote>
<p>Mir scheint es, als wenn dein Templateparameter MyClass*, und nicht MyClass ist.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> Recht hast du! Und das soll er auch bleiben. Wie umgehe ich das Problem hier?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/871008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/871008</guid><dc:creator><![CDATA[__gast]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:51:48 GMT</pubDate></item><item><title><![CDATA[Reply to In Templatekonstruktor Objekte erstellen on Tue, 13 Sep 2005 11:56:11 GMT]]></title><description><![CDATA[<p>Warum sollte er?</p>
<p>Naja wenn T ein MyClass* ist, dann legt new T auch ein MyClass* an und kein MyClass. Dein Vektor speichert zum Beispiel dann auch MyClass**. Willst Du das?? Ich glaube nicht. Deshalb -&gt; aus MyClass* MyClass machen. Wenn Du das Template auch noch anderweitig brauchst, spezialiser es für Pointer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/871012</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/871012</guid><dc:creator><![CDATA[7H3 N4C3R]]></dc:creator><pubDate>Tue, 13 Sep 2005 11:56:11 GMT</pubDate></item></channel></rss>