<?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[&amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor]]></title><description><![CDATA[<p>Hallo,</p>
<p>warum kann man keine Instanz einer Klasse, die keinen Default Konstruktor hat, in eine Map einfügen??? Das fogende Programm liefert mir zumindest immer den Compiler Gehler:<br />
g++ -c -o main.o main.cpp<br />
/usr/include/c++/3.2/bits/stl_map.h: In member function <code>\_Tp&amp; std::map&lt;_Key, \_Tp, \_Compare, \_Alloc&gt;::operator\[\](const \_Key&amp;) \[with \_Key = int, \_Tp = Base, \_Compare = std::less&lt;int&gt;, \_Alloc = std::allocator&lt;std::pair&lt;const int, Base&gt; &gt;\]': main.cpp:23: instantiated from here /usr/include/c++/3.2/bits/stl_map.h:225: no matching function for call to</code>Base<br />
::Base()'<br />
main.cpp:5: candidates are: Base::Base(const Base&amp;)<br />
main.cpp:7: Base::Base(int)</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;map&gt;

class Base
{
  public:
    Base(int v) : value(v) {};

    virtual~Base() {};

    int get_value() const { return value; };

  private:
    int value;
};

int main (int args, char **argv)
{
	typedef std::map&lt;int, Base&gt; map_t;
	map_t my_map;

	my_map[1] = Base(10);
}
</code></pre>
<p>Wie kann man das Problem beheben? (Default Konstruktor in die Klasse ist für mich keine Lösung)</p>
<p>Danke schon mal,</p>
<p>Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/149784/lt-map-gt-mit-klassen-ohne-default-konstruktor</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 08:38:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/149784.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 09 Jun 2006 10:29:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 10:29:06 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>warum kann man keine Instanz einer Klasse, die keinen Default Konstruktor hat, in eine Map einfügen??? Das fogende Programm liefert mir zumindest immer den Compiler Gehler:<br />
g++ -c -o main.o main.cpp<br />
/usr/include/c++/3.2/bits/stl_map.h: In member function <code>\_Tp&amp; std::map&lt;_Key, \_Tp, \_Compare, \_Alloc&gt;::operator\[\](const \_Key&amp;) \[with \_Key = int, \_Tp = Base, \_Compare = std::less&lt;int&gt;, \_Alloc = std::allocator&lt;std::pair&lt;const int, Base&gt; &gt;\]': main.cpp:23: instantiated from here /usr/include/c++/3.2/bits/stl_map.h:225: no matching function for call to</code>Base<br />
::Base()'<br />
main.cpp:5: candidates are: Base::Base(const Base&amp;)<br />
main.cpp:7: Base::Base(int)</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;map&gt;

class Base
{
  public:
    Base(int v) : value(v) {};

    virtual~Base() {};

    int get_value() const { return value; };

  private:
    int value;
};

int main (int args, char **argv)
{
	typedef std::map&lt;int, Base&gt; map_t;
	map_t my_map;

	my_map[1] = Base(10);
}
</code></pre>
<p>Wie kann man das Problem beheben? (Default Konstruktor in die Klasse ist für mich keine Lösung)</p>
<p>Danke schon mal,</p>
<p>Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074457</guid><dc:creator><![CDATA[ChrisSch]]></dc:creator><pubDate>Fri, 09 Jun 2006 10:29:06 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 10:32:24 GMT]]></title><description><![CDATA[<p>Dann musst du insert statt operator [] benutzen. operator[] erstellt nämlich ein Default-Objekt wenn das Objekt noch nicht in der Map existiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074464</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074464</guid><dc:creator><![CDATA[..........]]></dc:creator><pubDate>Fri, 09 Jun 2006 10:32:24 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 10:52:17 GMT]]></title><description><![CDATA[<p>Ja, so klappt es:</p>
<pre><code class="language-cpp">...

my_map.insert(map_t::value_type(1, Base(10));

...
</code></pre>
<p>Jetzt habe ich aber ein weiters Problem. Wenn ich für einen Schlüsselwert erneut &quot;insert&quot; aufrufe, wird die alte Instanz nicht von der neuen überschrieben. Ist das so gewollt? Gibt es eine elegantere Lösung als einfach vorher ein &quot;erase&quot; aufzurufen?</p>
<p>Gruß Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074484</guid><dc:creator><![CDATA[ChrisSch]]></dc:creator><pubDate>Fri, 09 Jun 2006 10:52:17 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 11:08:25 GMT]]></title><description><![CDATA[<p>Guck mal den Rückgabewert von insert an. Der gibt ein std::pair zurück wo drinsteht ob eingefügt einen Iterator. Mit diesem Iterator kannst du es einfügen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074502</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074502</guid><dc:creator><![CDATA[..........]]></dc:creator><pubDate>Fri, 09 Jun 2006 11:08:25 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 11:12:50 GMT]]></title><description><![CDATA[<p>Der Thread dürfte dich interessieren:</p>
<p><a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-33038-and-start-is-0-and-postdays-is-0-and-postorder-is-asc-and-highlight-is-.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-33038-and-start-is-0-and-postdays-is-0-and-postorder-is-asc-and-highlight-is-.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074510</guid><dc:creator><![CDATA[........]]></dc:creator><pubDate>Fri, 09 Jun 2006 11:12:50 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 11:24:58 GMT]]></title><description><![CDATA[<p>Wenn das Objekt bereits existiert, kannst du ja op[] verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074529</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074529</guid><dc:creator><![CDATA[Dr. Prof]]></dc:creator><pubDate>Fri, 09 Jun 2006 11:24:58 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 11:26:41 GMT]]></title><description><![CDATA[<p>Dr. Prof schrieb:</p>
<blockquote>
<p>Wenn das Objekt bereits existiert, kannst du ja op[] verwenden.</p>
</blockquote>
<p>Ne das geht nicht. Dann müsste ja wieder ein Default-Konstruktor zur Verfügung stehen. Das wird ja zur Compile-Zeit schon geprüft.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074530</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074530</guid><dc:creator><![CDATA[.........]]></dc:creator><pubDate>Fri, 09 Jun 2006 11:26:41 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;map&amp;gt; mit Klassen ohne Default Konstruktor on Fri, 09 Jun 2006 11:32:34 GMT]]></title><description><![CDATA[<p>Da mußt du notfalls selber prüfen, ob das Element schon drin war (per find()) und je nach Ergebnis entweder einfügen oder überschreiben.</p>
<p>(btw, insert() gibt ein pair&lt;iterator,bool&gt; zurück - wenn das zweite Element false ist, war der Wert schon da (und das erste Element enthaält einen Iterator darauf)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1074534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1074534</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 09 Jun 2006 11:32:34 GMT</pubDate></item></channel></rss>