<?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[Methode nicht als konstant deklarierbar]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse &quot;partition_tree&quot; (nur das wesentliche)</p>
<pre><code class="language-cpp">class partition_tree
{
private:
	map&lt;int, node*&gt; nodes_sep;
public:
	const node* get_node(const int i) const;
};
</code></pre>
<p>wobei</p>
<pre><code class="language-cpp">const node* partition_tree::get_node(const int i) const
{
	return nodes_sep[i];
}
</code></pre>
<p>Hierbei ist &quot;node&quot; irgendeine weitere Klasse. Der Compiler gibt nun bezüglich der Zeile</p>
<pre><code class="language-cpp">return nodes_sep[i];
</code></pre>
<p>die Fehlermeldung</p>
<blockquote>
<p>passing ‘const std::map&lt;int, node*, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;const int, node*&gt; &gt; &gt;’ as ‘this’ argument of ‘_Tp&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](const _Key&amp;) [with _Key = int, _Tp = node*, _Compare = std::less&lt;int&gt;, _Alloc = std::allocator&lt;std::pair&lt;const int, node*&gt; &gt;]’ discards qualifiers</p>
</blockquote>
<p>aus. Wenn ich &quot;partition_tree::get_node&quot; nicht als konstant deklariere, tritt der Fehler nicht auf. Ich sehe jedoch nicht, inwieweit die Methode &quot;partition_tree::get_node&quot;, die Klasse &quot;partition_tree&quot; verändern soll. Weiß jemand, was hier los ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/206836/methode-nicht-als-konstant-deklarierbar</link><generator>RSS for Node</generator><lastBuildDate>Sat, 10 Oct 2026 15:53:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/206836.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 29 Feb 2008 14:53:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Methode nicht als konstant deklarierbar on Fri, 29 Feb 2008 14:53:35 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse &quot;partition_tree&quot; (nur das wesentliche)</p>
<pre><code class="language-cpp">class partition_tree
{
private:
	map&lt;int, node*&gt; nodes_sep;
public:
	const node* get_node(const int i) const;
};
</code></pre>
<p>wobei</p>
<pre><code class="language-cpp">const node* partition_tree::get_node(const int i) const
{
	return nodes_sep[i];
}
</code></pre>
<p>Hierbei ist &quot;node&quot; irgendeine weitere Klasse. Der Compiler gibt nun bezüglich der Zeile</p>
<pre><code class="language-cpp">return nodes_sep[i];
</code></pre>
<p>die Fehlermeldung</p>
<blockquote>
<p>passing ‘const std::map&lt;int, node*, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;const int, node*&gt; &gt; &gt;’ as ‘this’ argument of ‘_Tp&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](const _Key&amp;) [with _Key = int, _Tp = node*, _Compare = std::less&lt;int&gt;, _Alloc = std::allocator&lt;std::pair&lt;const int, node*&gt; &gt;]’ discards qualifiers</p>
</blockquote>
<p>aus. Wenn ich &quot;partition_tree::get_node&quot; nicht als konstant deklariere, tritt der Fehler nicht auf. Ich sehe jedoch nicht, inwieweit die Methode &quot;partition_tree::get_node&quot;, die Klasse &quot;partition_tree&quot; verändern soll. Weiß jemand, was hier los ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1465543</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1465543</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Fri, 29 Feb 2008 14:53:35 GMT</pubDate></item><item><title><![CDATA[Reply to Methode nicht als konstant deklarierbar on Fri, 29 Feb 2008 14:57:06 GMT]]></title><description><![CDATA[<p>probier mal</p>
<pre><code class="language-cpp">return nodes_sep.find(i)-&gt;second;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1465545</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1465545</guid><dc:creator><![CDATA[Kleiner Helfer]]></dc:creator><pubDate>Fri, 29 Feb 2008 14:57:06 GMT</pubDate></item><item><title><![CDATA[Reply to Methode nicht als konstant deklarierbar on Fri, 29 Feb 2008 15:17:28 GMT]]></title><description><![CDATA[<p>besser (falls i evtl kein gueltiger key...):</p>
<pre><code class="language-cpp">//bei kleiner map size
return (nodes_sep.find(i)!=nodes_sep.end() ? nodes_sep.find(i)-&gt;second : NULL;

//ansonsten mit tmp val arbeiten:
std::map&lt;int, node*&gt;::cons_iterator pos=nodes_sep.find(i);
if( pos== nodes_sep.end() ) return NULL;
return pos-&gt;second;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1465552</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1465552</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Fri, 29 Feb 2008 15:17:28 GMT</pubDate></item><item><title><![CDATA[Reply to Methode nicht als konstant deklarierbar on Fri, 29 Feb 2008 15:39:39 GMT]]></title><description><![CDATA[<p>als kleiner Zusatz die Fehlermeldung von MSVC 2008 bzgl. deiner Ursprungsversion:</p>
<blockquote>
<p>: error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map&lt;_Kty,_Ty&gt;' (or there is no acceptable conversion)<br />
with<br />
[<br />
_Kty=int,<br />
_Ty=node *<br />
]<br />
VC\include\map(167): could be 'node *&amp;std::map&lt;_Kty,_Ty&gt;::operator [](const int &amp;)'<br />
with<br />
[<br />
_Kty=int,<br />
_Ty=node *<br />
]<br />
while trying to match the argument list '(const std::map&lt;_Kty,_Ty&gt;, const int)'<br />
with<br />
[<br />
_Kty=int,<br />
_Ty=node *<br />
]</p>
</blockquote>
<p>wie du siehst greifst du per reference auf den node-ptr in der map bei Verwednung des index-op zu. Das ist in const - Methoden unzulässig, denn du koenntest das objekt ja verändern <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="🙂"
    /> Mit find erhälst du den node als Kopie in einem std::pair... das ist wiederum zulässig</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1465579</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1465579</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Fri, 29 Feb 2008 15:39:39 GMT</pubDate></item><item><title><![CDATA[Reply to Methode nicht als konstant deklarierbar on Fri, 29 Feb 2008 16:38:23 GMT]]></title><description><![CDATA[<p>der indexoperator einer map liefert dir *immer* eine gültige referenz zurück. falls er den schlüssel in der map nicht findet, wird einfach ein neuer angelegt und ein default-wert für ihn angelegt - d.h. operator[] muss die map verändern dürfen.</p>
<p>im übrigen solltest du bei <code>nodes_sep.find(i)</code> deshalb auch überprüfen, ob der iterator auf ein gültiges element zeigt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1465634</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1465634</guid><dc:creator><![CDATA[queer_boy]]></dc:creator><pubDate>Fri, 29 Feb 2008 16:38:23 GMT</pubDate></item><item><title><![CDATA[Reply to Methode nicht als konstant deklarierbar on Fri, 29 Feb 2008 16:44:48 GMT]]></title><description><![CDATA[<p>genau. und das bedeutet, dass bei <strong>return nodes_sep[i]</strong> und einem nicht vorhandenem Schluessel die map automatisch erweitert werden wuerde. das ist in einer cont-methode jedoch verboten. selbst bei eine rnicht const methode ist das gefährlich, da hier evtl ein ungueltiger pointer zurueck gegeben wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1465642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1465642</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Fri, 29 Feb 2008 16:44:48 GMT</pubDate></item></channel></rss>