<?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[Binärer Operator funktioniert nicht]]></title><description><![CDATA[<p>Hallo!<br />
Ich hab da so eine Klasse (für komplexe Zahlen) und einen Operator für Multiplikation:</p>
<pre><code class="language-cpp">class t_complex
{
public:
    //...
    //blah blah
    //...
    t_complex &amp;operator*(const t_complex &amp;c)
    {
        //... blah
    }
};
</code></pre>
<p>Funktioniert auch alles wunderbar mit der Multiplikation.<br />
Nun hab ich ja aber auch noch eine Klasse, die sieht so aus:</p>
<pre><code class="language-cpp">template &lt;class T, int height, int length&gt;
class t_matrix
{
public:
    T array[height][length];
    //...
};
</code></pre>
<p>Und für diese Klasse hab ich auch so einen schönen Operator (Multiplikation der Matrix mit einem Skalar):</p>
<pre><code class="language-cpp">template &lt;class T, int length, int height&gt;
t_matrix&lt;T, height, length&gt; operator*(const T &amp;c, const t_matrix&lt;T, height, length&gt; &amp;m)
{
	t_matrix&lt;T, height, length&gt; ret;
	for (int i=0; i&lt;height; i++)
		for (int j=0; j&lt;length; j++)
			ret.array[i][j]=c*m.array[i][j];
	return ret;
}
</code></pre>
<p>Wenn ich nun aber so eine Matrix (aus komplexen Zahlen) mit einer komplexen Zahl multiplizieren will, meckert der Compiler bei folgender Zeile:</p>
<pre><code class="language-cpp">ret.array[i][j]=c*m.array[i][j];
</code></pre>
<p>Die Einträge des Array sind ja vom Typ t_complex, genau wie c.<br />
Der Compiler erzählt mir folgendes:</p>
<pre><code>error C2678: binary '*' : no operator found which takes a left-hand operand of type 'const t_complex' (or there is no acceptable conversion)
</code></pre>
<p>Das finde ich nicht nett.<br />
Wenn ich allerdings die komplexe Multiplikation außerhalb des Klassenkörpers definiere, wie folgt</p>
<pre><code class="language-cpp">t_complex operator*(const t_complex &amp;c1, const t_complex &amp;c2)
{
		t_real re;
		t_real im;
		re=c1.real*c2.real-c2.imag*c1.imag;
		im=c1.imag*c2.real+c2.imag*c1.real;
		return t_complex(re, im);
}
</code></pre>
<p>, dann meckert er nicht.<br />
Kann mir jemand erklären, was das soll? <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="😕"
    /><br />
Danke schon mal im Voraus,<br />
Moritz</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/154297/binärer-operator-funktioniert-nicht</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 22:29:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/154297.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 Jul 2006 01:39:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Binärer Operator funktioniert nicht on Wed, 26 Jul 2006 01:39:12 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Ich hab da so eine Klasse (für komplexe Zahlen) und einen Operator für Multiplikation:</p>
<pre><code class="language-cpp">class t_complex
{
public:
    //...
    //blah blah
    //...
    t_complex &amp;operator*(const t_complex &amp;c)
    {
        //... blah
    }
};
</code></pre>
<p>Funktioniert auch alles wunderbar mit der Multiplikation.<br />
Nun hab ich ja aber auch noch eine Klasse, die sieht so aus:</p>
<pre><code class="language-cpp">template &lt;class T, int height, int length&gt;
class t_matrix
{
public:
    T array[height][length];
    //...
};
</code></pre>
<p>Und für diese Klasse hab ich auch so einen schönen Operator (Multiplikation der Matrix mit einem Skalar):</p>
<pre><code class="language-cpp">template &lt;class T, int length, int height&gt;
t_matrix&lt;T, height, length&gt; operator*(const T &amp;c, const t_matrix&lt;T, height, length&gt; &amp;m)
{
	t_matrix&lt;T, height, length&gt; ret;
	for (int i=0; i&lt;height; i++)
		for (int j=0; j&lt;length; j++)
			ret.array[i][j]=c*m.array[i][j];
	return ret;
}
</code></pre>
<p>Wenn ich nun aber so eine Matrix (aus komplexen Zahlen) mit einer komplexen Zahl multiplizieren will, meckert der Compiler bei folgender Zeile:</p>
<pre><code class="language-cpp">ret.array[i][j]=c*m.array[i][j];
</code></pre>
<p>Die Einträge des Array sind ja vom Typ t_complex, genau wie c.<br />
Der Compiler erzählt mir folgendes:</p>
<pre><code>error C2678: binary '*' : no operator found which takes a left-hand operand of type 'const t_complex' (or there is no acceptable conversion)
</code></pre>
<p>Das finde ich nicht nett.<br />
Wenn ich allerdings die komplexe Multiplikation außerhalb des Klassenkörpers definiere, wie folgt</p>
<pre><code class="language-cpp">t_complex operator*(const t_complex &amp;c1, const t_complex &amp;c2)
{
		t_real re;
		t_real im;
		re=c1.real*c2.real-c2.imag*c1.imag;
		im=c1.imag*c2.real+c2.imag*c1.real;
		return t_complex(re, im);
}
</code></pre>
<p>, dann meckert er nicht.<br />
Kann mir jemand erklären, was das soll? <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="😕"
    /><br />
Danke schon mal im Voraus,<br />
Moritz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1104268</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1104268</guid><dc:creator><![CDATA[Krecik]]></dc:creator><pubDate>Wed, 26 Jul 2006 01:39:12 GMT</pubDate></item><item><title><![CDATA[Reply to Binärer Operator funktioniert nicht on Wed, 26 Jul 2006 04:38:33 GMT]]></title><description><![CDATA[<p>Dein Member-operator ist nicht als const deklariert, daher kann er nur angewendet werden, wenn der linke Operand (*this) nicht const ist. Mach's doch mal so:</p>
<pre><code class="language-cpp">t_complex &amp;operator*(const t_complex &amp;c) const
{
    // ...
</code></pre>
<p>Dass der Operator eine Referenz zurückgibt, lässt mich übrigens auf einen Fehler schließen. Entweder gibst du eine Referenz auf eine lokale Variable zurück, oder änderst das Objekt selbst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1104275</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1104275</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Wed, 26 Jul 2006 04:38:33 GMT</pubDate></item><item><title><![CDATA[Reply to Binärer Operator funktioniert nicht on Wed, 26 Jul 2006 13:16:09 GMT]]></title><description><![CDATA[<p>MFK schrieb:</p>
<blockquote>
<p>Dein Member-operator ist nicht als const deklariert, daher kann er nur angewendet werden, wenn der linke Operand (*this) nicht const ist. Mach's doch mal so:</p>
<pre><code class="language-cpp">t_complex &amp;operator*(const t_complex &amp;c) const
{
    // ...
</code></pre>
</blockquote>
<p>Ach so geht das. Jetzt funktioniert's. Danke.</p>
<p>MFK schrieb:</p>
<blockquote>
<p>Dass der Operator eine Referenz zurückgibt, lässt mich übrigens auf einen Fehler schließen. Entweder gibst du eine Referenz auf eine lokale Variable zurück, oder änderst das Objekt selbst.</p>
</blockquote>
<p>Ja, das hatte ich nur falsch abgetippt. Steht gar nicht so als Referenz in meinem Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1104611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1104611</guid><dc:creator><![CDATA[Krecik]]></dc:creator><pubDate>Wed, 26 Jul 2006 13:16:09 GMT</pubDate></item></channel></rss>