<?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[ambiguous overloading]]></title><description><![CDATA[<p>Ich habe in meiner Klasse Zugriffsoperatoren erstellt:</p>
<pre><code class="language-cpp">template &lt;class T&gt;
T&amp; Vector&lt;T&gt;::operator[](unsigned int index) {
	assert(index &lt; this-&gt;m_vData.size() );

	return this-&gt;m_vData[index];
}

template &lt;class T&gt;
const T&amp; Vector&lt;T&gt;::operator[](unsigned int index) const {
	assert(index &lt; this-&gt;m_vData.size() );

	return this-&gt;m_vData[index];
}
</code></pre>
<p>Nun habe ich von dieser Klasse abgeleitet und will diese Operatoren benutzen:</p>
<pre><code class="language-cpp">template &lt;class T&gt;
Vec3&lt;T&gt;::Vec3(T init_x, T init_y, T init_z) {
	this-&gt;m_vData.resize(3);
	(*this)[0] = init_x; // FEHLER
	(*this)[1] = init_y; // FEHLER
	(*this)[2] = init_z; // FEHLER
}
/*
 * ODER SO
 */
template &lt;class T&gt;
Vec3&lt;T&gt; Vec3&lt;T&gt;::GetRotatedY(double angle) const
{
	assert(this-&gt;m_vData.size() == 3);

	if(angle==0.0)
		return (*this);

	float sinAngle=(float)sin(M_PI*angle/180);
	float cosAngle=(float)cos(M_PI*angle/180);

	Vec3&lt;T&gt; result(*this);
	result[0] = result[0]*cosAngle + result[2]*sinAngle; // FEHLER
	result[1] = result[1]; // FEHLER
	result[2] = -result[0]*sinAngle + result[2]*cosAngle; // FEHLER

	return result;
}
</code></pre>
<p>Leider bekomme ich an jeder Stelle, in der ich die Dinger so anwende eine komische Meldung wie:</p>
<pre><code>../src/math/Vec3.hpp:116: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
../src/math/Vector.hpp:179: note: candidate 1: T&amp; MATH::Vector&lt;T&gt;::operator[](unsigned int) [with T = float]
../src/math/Vec3.hpp:116: note: candidate 2: operator[](float*, int) &lt;built-in&gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/234095/ambiguous-overloading</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 11:41:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/234095.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 13 Feb 2009 00:46:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ambiguous overloading on Fri, 13 Feb 2009 00:47:37 GMT]]></title><description><![CDATA[<p>Ich habe in meiner Klasse Zugriffsoperatoren erstellt:</p>
<pre><code class="language-cpp">template &lt;class T&gt;
T&amp; Vector&lt;T&gt;::operator[](unsigned int index) {
	assert(index &lt; this-&gt;m_vData.size() );

	return this-&gt;m_vData[index];
}

template &lt;class T&gt;
const T&amp; Vector&lt;T&gt;::operator[](unsigned int index) const {
	assert(index &lt; this-&gt;m_vData.size() );

	return this-&gt;m_vData[index];
}
</code></pre>
<p>Nun habe ich von dieser Klasse abgeleitet und will diese Operatoren benutzen:</p>
<pre><code class="language-cpp">template &lt;class T&gt;
Vec3&lt;T&gt;::Vec3(T init_x, T init_y, T init_z) {
	this-&gt;m_vData.resize(3);
	(*this)[0] = init_x; // FEHLER
	(*this)[1] = init_y; // FEHLER
	(*this)[2] = init_z; // FEHLER
}
/*
 * ODER SO
 */
template &lt;class T&gt;
Vec3&lt;T&gt; Vec3&lt;T&gt;::GetRotatedY(double angle) const
{
	assert(this-&gt;m_vData.size() == 3);

	if(angle==0.0)
		return (*this);

	float sinAngle=(float)sin(M_PI*angle/180);
	float cosAngle=(float)cos(M_PI*angle/180);

	Vec3&lt;T&gt; result(*this);
	result[0] = result[0]*cosAngle + result[2]*sinAngle; // FEHLER
	result[1] = result[1]; // FEHLER
	result[2] = -result[0]*sinAngle + result[2]*cosAngle; // FEHLER

	return result;
}
</code></pre>
<p>Leider bekomme ich an jeder Stelle, in der ich die Dinger so anwende eine komische Meldung wie:</p>
<pre><code>../src/math/Vec3.hpp:116: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
../src/math/Vector.hpp:179: note: candidate 1: T&amp; MATH::Vector&lt;T&gt;::operator[](unsigned int) [with T = float]
../src/math/Vec3.hpp:116: note: candidate 2: operator[](float*, int) &lt;built-in&gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1662892</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1662892</guid><dc:creator><![CDATA[gentoo]]></dc:creator><pubDate>Fri, 13 Feb 2009 00:47:37 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguous overloading on Fri, 13 Feb 2009 06:05:51 GMT]]></title><description><![CDATA[<p>kann das sein dass du in der Klassendefinition von Vector&lt;T&gt; einen Konvertierungsoperator nach T* eingebaut hast? Falls ja: Weg damit!<br />
Der Compiler scheint nämlich ganz Standardkonform nicht entscheiden zu wollen, ob er lieber das Argument (ein int) in ein unsigned int konvertieren soll, oder ob er besser den Ausdruck result (den Vector&lt;float&gt; in deinem Fall) in ein float* konvertieren soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1662914</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1662914</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 13 Feb 2009 06:05:51 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguous overloading on Fri, 13 Feb 2009 10:58:48 GMT]]></title><description><![CDATA[<p>krass, ich habe so einen Operator eingebaut. Aber den wollte ich später auch noch nutzen ):<br />
Aber ich wäre niemals drauf gekommen, dass es daran liegen könnte. Kann man irgendwie einen Typkonverter für float* in diese templateklasse stecken ohne, dass es Probleme gibt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1663026</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663026</guid><dc:creator><![CDATA[gentoo]]></dc:creator><pubDate>Fri, 13 Feb 2009 10:58:48 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguous overloading on Fri, 13 Feb 2009 13:44:22 GMT]]></title><description><![CDATA[<p>gentoo schrieb:</p>
<blockquote>
<p>Aber ich wäre niemals drauf gekommen, dass es daran liegen könnte. Kann man irgendwie einen Typkonverter für float* in diese templateklasse stecken ohne, dass es Probleme gibt?</p>
</blockquote>
<p>Warum nicht eine einfache Memberfunktion?</p>
<p>Konvertierungsoperatoren sollten sowieso mit Bedacht eingesetzt werden. Man muss sich bewusst sein, dass es dann wirklich viele Kontexte gibt, wo eine Umwandlung implizit stattfinden kann. Und nicht selten führt das zu Problemen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1663199</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663199</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 13 Feb 2009 13:44:22 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguous overloading on Fri, 13 Feb 2009 14:24:02 GMT]]></title><description><![CDATA[<p>Lass ihn raus. Wie du siehst zieht der Compiler die Konvertierungen immer mit in Betracht und nicht nur dann wann du sie gerne hättest. Wozu wolltest du sie denn nutzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1663235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663235</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 13 Feb 2009 14:24:02 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguous overloading on Sat, 14 Feb 2009 13:40:08 GMT]]></title><description><![CDATA[<p>ach ich wollte opengl, welches häufig float arrays benutzt mein object einfach so übergeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1663727</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663727</guid><dc:creator><![CDATA[gentoo]]></dc:creator><pubDate>Sat, 14 Feb 2009 13:40:08 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguous overloading on Sat, 14 Feb 2009 14:28:54 GMT]]></title><description><![CDATA[<p>gentoo schrieb:</p>
<blockquote>
<p>ach ich wollte opengl, welches häufig float arrays benutzt mein object einfach so übergeben.</p>
</blockquote>
<p>Das geht auch mit normalen Funktionen ganz gut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1663750</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663750</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sat, 14 Feb 2009 14:28:54 GMT</pubDate></item></channel></rss>