<?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[Templates]]></title><description><![CDATA[<p>hey,<br />
ich hab schon wieder eine Frage...<br />
ich hab hier folgendes kleines Programm:</p>
<pre><code class="language-csharp">#include &lt;iostream&gt;
using namespace std;

template &lt;class T&gt; T max(T x, T y) {
	if (x&gt;y)
		return x;
	else
		return y;
}

int main() {
	int a=5;
	int b=8;
	double c=12.3;
	double d=18.7;
	cout &lt;&lt; max(a,b) &lt;&lt; &quot;\n&quot;;
	cout &lt;&lt; max(c,d) &lt;&lt; &quot;\n&quot;;
	return 0;
}
</code></pre>
<p>Wenn ich das kompilieren will, dann funktioniert das nicht - aber ich versteh einfach nicht, wo das Problem liegt...</p>
<p>Das hier spuckt der compiler aus:</p>
<pre><code class="language-csharp">test3.cpp: In function ‘int main()’:
test3.cpp:16: error: call of overloaded ‘max(int&amp;, int&amp;)’ is ambiguous
test3.cpp:4: note: candidates are: T max(T, T) [with T = int]
/usr/include/c++/4.0.0/bits/stl_algobase.h:206: note:                 const _Tp&amp; std::max(const _Tp&amp;, const _Tp&amp;) [with _Tp = int]
test3.cpp:17: error: call of overloaded ‘max(double&amp;, double&amp;)’ is ambiguous
test3.cpp:4: note: candidates are: T max(T, T) [with T = double]
/usr/include/c++/4.0.0/bits/stl_algobase.h:206: note:                 const _Tp&amp; std::max(const _Tp&amp;, const _Tp&amp;) [with _Tp = double]
</code></pre>
<p>Kann mir da einer helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/234337/templates</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 11:41:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/234337.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 Feb 2009 16:16:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:37:28 GMT]]></title><description><![CDATA[<p>hey,<br />
ich hab schon wieder eine Frage...<br />
ich hab hier folgendes kleines Programm:</p>
<pre><code class="language-csharp">#include &lt;iostream&gt;
using namespace std;

template &lt;class T&gt; T max(T x, T y) {
	if (x&gt;y)
		return x;
	else
		return y;
}

int main() {
	int a=5;
	int b=8;
	double c=12.3;
	double d=18.7;
	cout &lt;&lt; max(a,b) &lt;&lt; &quot;\n&quot;;
	cout &lt;&lt; max(c,d) &lt;&lt; &quot;\n&quot;;
	return 0;
}
</code></pre>
<p>Wenn ich das kompilieren will, dann funktioniert das nicht - aber ich versteh einfach nicht, wo das Problem liegt...</p>
<p>Das hier spuckt der compiler aus:</p>
<pre><code class="language-csharp">test3.cpp: In function ‘int main()’:
test3.cpp:16: error: call of overloaded ‘max(int&amp;, int&amp;)’ is ambiguous
test3.cpp:4: note: candidates are: T max(T, T) [with T = int]
/usr/include/c++/4.0.0/bits/stl_algobase.h:206: note:                 const _Tp&amp; std::max(const _Tp&amp;, const _Tp&amp;) [with _Tp = int]
test3.cpp:17: error: call of overloaded ‘max(double&amp;, double&amp;)’ is ambiguous
test3.cpp:4: note: candidates are: T max(T, T) [with T = double]
/usr/include/c++/4.0.0/bits/stl_algobase.h:206: note:                 const _Tp&amp; std::max(const _Tp&amp;, const _Tp&amp;) [with _Tp = double]
</code></pre>
<p>Kann mir da einer helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664780</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664780</guid><dc:creator><![CDATA[Wanderfalke]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:37:28 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:20:03 GMT]]></title><description><![CDATA[<p>Ja, dank deinem using namespace std gibts in dem Scope zwei Funktionen max. Benutze ::max oder nimm das using namespace std weg. <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/1664783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664783</guid><dc:creator><![CDATA[*rant*]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:20:03 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:23:12 GMT]]></title><description><![CDATA[<p>max gibts auch im namespace std::<br />
Du ziehst Dir mit <code>using namespace std</code> alles aus std:: in den globalen Namensraum. Daher gibts Konflikte.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
//using namespace std; &lt;- kein using namespace!

template &lt;class T&gt; T max(T x, T y) {
	if (x&gt;y)
		return x;
	else
		return y;
}

int main() {
	int a=5;
	int b=8;
	double c=12.3;
	double d=18.7;
	std::cout &lt;&lt; max(a,b) &lt;&lt; &quot;\n&quot;; //explizit std:: mit angeben
	std::cout &lt;&lt; max(c,d) &lt;&lt; &quot;\n&quot;; //dito
	return 0;
}
</code></pre>
<p>Alternativ: Deine Funktion anders nennen, z.B. myMax</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664787</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:23:12 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:33:06 GMT]]></title><description><![CDATA[<p>hmm ja ihr habt Recht - mit den Änderungen funktioniert das Programm. Aber ich verstehs irgendwie nicht wirklich.</p>
<p>Ich habe mir mit dem template doch eine allgemeine Funktion max gebastelt. Und jetzt ruf ich sie weiter unten auf mit max(a,b). Dann müsste er doch die Funktion aufrufen und die Werte ausspucken. Wieso muss dann der Name geändert werden?</p>
<p>Komischerweise hab ich grad gemerkt, dass das Programm auch funktioniert, wenn ich sowas hinschreibe:</p>
<pre><code class="language-csharp">template &lt;class T&gt; T dasisteinkomischertest(T x,T y) {
</code></pre>
<p>wieso kann das denn funktionieren?? <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="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664795</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664795</guid><dc:creator><![CDATA[Wanderfalke]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:33:06 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:45:32 GMT]]></title><description><![CDATA[<p>Wanderfalke schrieb:</p>
<blockquote>
<p>hmm ja ihr habt Recht - mit den Änderungen funktioniert das Programm. Aber ich verstehs irgendwie nicht wirklich.</p>
</blockquote>
<p>Was verstehst du nicht? Der Compiler findet nunmal 2 verschiedene max-Funktionen:<br />
Da du using namespace std verwendet hast sieht er einmal die std::max und einmal dein max, und kann nicht wissen welche du aufrufen willst.</p>
<p>Und logischerweise funktioniert dein Programm wenn du deine Funktion ausdokumentierst, da er dann keinen Namenskonflikt hat, und gleich die max-Funktion der Standardbibliothek aufruft.</p>
<p>cu André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664802</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664802</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:45:32 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:53:12 GMT]]></title><description><![CDATA[<p>also kann man eigentlich sagen, dass es ziemlich bescheuert war, die Funktion max zu nennen. Die Folge davon, dass ich da ::max hingeschrieben hab, war doch nur, dass er die max-Funktion aus der Standardbibliothek genommen hat und das template links liegen gelassen hat, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664809</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664809</guid><dc:creator><![CDATA[Wanderfalke]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:53:12 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 16:58:36 GMT]]></title><description><![CDATA[<p>Wanderfalke schrieb:</p>
<blockquote>
<p>also kann man eigentlich sagen, dass es ziemlich bescheuert war, die Funktion max zu nennen...</p>
</blockquote>
<p>Nein, eher das du using namespace eingesetzt hast, ohne dir die Folgen zu verinnerlichen. Wenn ich eine Funktion schreiben würde, die einen Maximalwert zurückliefert, würde ich diese auch max nennen, doch läge sie in einem anderen Namensraum.</p>
<p>using namespace ist solange ganz okay, solange man Mehrdeutigkeiten ausschließen kann (und man es nicht im Header verwendet, da es sonst global gilt...).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664815</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664815</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Mon, 16 Feb 2009 16:58:36 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 17:35:45 GMT]]></title><description><![CDATA[<p>Wanderfalke schrieb:</p>
<blockquote>
<p>Die Folge davon, dass ich da ::max hingeschrieben hab, war doch nur, dass er die max-Funktion aus der Standardbibliothek genommen hat und das template links liegen gelassen hat, oder?</p>
</blockquote>
<p>Nein, ::max ist die Funktion auf globaler Ebene (also Deine). Die aus der Standardbibliothek wäre std::max.</p>
<p>Nur ist durch das using namespace std die Funktion std::max ebenfalls im globalen Namensraum bekannt gemacht worden. Damit kann &quot;max&quot; (ohne Namespace-Angabe) entweder ::max oder std::max sein, der Compiler weiss nicht, welche von beiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1664841</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1664841</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Mon, 16 Feb 2009 17:35:45 GMT</pubDate></item><item><title><![CDATA[Reply to Templates on Mon, 16 Feb 2009 22:53:09 GMT]]></title><description><![CDATA[<p>ah ok - vielen Dank! <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/1665015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1665015</guid><dc:creator><![CDATA[Wanderfalke]]></dc:creator><pubDate>Mon, 16 Feb 2009 22:53:09 GMT</pubDate></item></channel></rss>