<?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[template operatoren&#x2F;funktionen]]></title><description><![CDATA[<p>Hi,<br />
kann mir jemand sagen, warum diese Dekleration funktioniert:</p>
<pre><code class="language-cpp">friend Matrix&lt;T&gt; operator+ &lt;T&gt;(const Matrix&lt;T&gt; &amp;A, const Matrix&lt;T&gt; &amp;B);
</code></pre>
<p>diese aber nicht:</p>
<pre><code class="language-cpp">friend Matrix&lt;T&gt; operator* &lt;T&gt;(const Matrix&lt;T&gt; &amp;A, const Matrix&lt;T&gt; &amp;B);
</code></pre>
<p>wenn ich das ganze mit einer funktion versuche, klappts auch nicht:</p>
<pre><code class="language-cpp">friend Matrix&lt;T&gt; matrixmult &lt;T&gt;(const Matrix&lt;T&gt; &amp;A, const Matrix&lt;T&gt; &amp;B);
</code></pre>
<p>Matrix&lt;T&gt; ist meine Template Klasse, in der ich die oben genannten Operatoren/Funktionen deklarieren möchte.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/228080/template-operatoren-funktionen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 29 Sep 2026 17:29:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/228080.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Nov 2008 19:28:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Sun, 23 Nov 2008 19:28:33 GMT]]></title><description><![CDATA[<p>Hi,<br />
kann mir jemand sagen, warum diese Dekleration funktioniert:</p>
<pre><code class="language-cpp">friend Matrix&lt;T&gt; operator+ &lt;T&gt;(const Matrix&lt;T&gt; &amp;A, const Matrix&lt;T&gt; &amp;B);
</code></pre>
<p>diese aber nicht:</p>
<pre><code class="language-cpp">friend Matrix&lt;T&gt; operator* &lt;T&gt;(const Matrix&lt;T&gt; &amp;A, const Matrix&lt;T&gt; &amp;B);
</code></pre>
<p>wenn ich das ganze mit einer funktion versuche, klappts auch nicht:</p>
<pre><code class="language-cpp">friend Matrix&lt;T&gt; matrixmult &lt;T&gt;(const Matrix&lt;T&gt; &amp;A, const Matrix&lt;T&gt; &amp;B);
</code></pre>
<p>Matrix&lt;T&gt; ist meine Template Klasse, in der ich die oben genannten Operatoren/Funktionen deklarieren möchte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619598</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619598</guid><dc:creator><![CDATA[Kleriker]]></dc:creator><pubDate>Sun, 23 Nov 2008 19:28:33 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 07:25:26 GMT]]></title><description><![CDATA[<p>1. Was heisst für dich funktionieren?</p>
<p>2. Compilerangaben, sowie einen besseren Kontext wäre auch hilfreich</p>
<p>3. Ist Matrix ein Klassentemplate und keine Templateklasse</p>
<p>4. Was soll das &lt;T&gt; nach dem Operator überhaupt? - Was soll das bezwecken?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619723</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619723</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 24 Nov 2008 07:25:26 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 12:26:23 GMT]]></title><description><![CDATA[<p>Sorry, dachte es gäbe möglicherweise einen allgemeinen unterschied zwischen dem operator * und +</p>
<p>was deine 4. frage betrifft:<br />
das &lt;T&gt; nach dem operator definiert damit ein operatortemplate (<a href="http://www.hlrs.de/people/mueller/tutorials/script/scriptse22.html" rel="nofollow">http://www.hlrs.de/people/mueller/tutorials/script/scriptse22.html</a>)</p>
<p>also meine Matrix Klasse sieht etwa so aus:<br />
( Matrix greift auf die TNT library zu <a href="http://math.nist.gov/tnt/tnt_3_0_11.zip" rel="nofollow">http://math.nist.gov/tnt/tnt_3_0_11.zip</a>)</p>
<pre><code class="language-cpp">#include &quot;tnt.h&quot; 
#include &lt;assert.h&gt;

template &lt;class temp&gt;
class Matrix
{
private:
    unsigned long itsRows; 
    unsigned long itsColumns; 
	TNT::Array2D&lt;temp&gt; itsArray2D; // no pointer

public:
    Matrix(unsigned long rows, unsigned long columns);
    ~Matrix();

    //operator
    inline temp&amp; operator()(unsigned long i, unsigned long j);
	inline Matrix&lt;temp&gt; &amp; Matrix&lt;temp&gt;::operator=(const temp &amp;a);
	friend std::ostream&amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp;s, const Matrix&lt;temp&gt; &amp;matrix1);
	friend std::istream&amp; operator&gt;&gt; &lt;&gt;(std::istream &amp;s, Matrix&lt;temp&gt; &amp;A);

	friend Matrix&lt;temp&gt; operator+ &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B);
	friend Matrix&lt;temp&gt; operator- &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B);

};

// C O N S T R U C T O R  for matrixes
template&lt;class temp&gt;
Matrix&lt;temp&gt;::Matrix(unsigned long rows, unsigned long columns)
  : itsRows(rows)
  , itsColumns(columns)
  , itsArray2D(rows, columns)
{
  // object directly initialized
}

// D E S T R U C T O R  for matrixes
template&lt;class temp&gt;
Matrix&lt;temp&gt;::~Matrix(){}

//O P E R A T O R  overloading
// Operator ()
template&lt;class temp&gt;
inline temp&amp; Matrix&lt;temp&gt;::operator ()(unsigned long i, unsigned long j)
{
	assert(i &gt;= 0);
	assert(j &gt;= 0);
	assert(i &lt; itsRows);
	assert(j &lt; itsColumns);
    return itsArray2D[i][j];
}

// Operator =
template &lt;class temp&gt;
Matrix&lt;temp&gt; &amp; Matrix&lt;temp&gt;::operator=(const temp &amp;a)
{
	itsArray2D = a;
	return *this;
}

// Operator &lt;&lt;
template &lt;class temp&gt;
std::ostream&amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp;s, const Matrix&lt;temp&gt; &amp;A)
{
    ...
    return s;
}

// Operator &gt;&gt;
template &lt;class temp&gt;
std::istream&amp; operator&gt;&gt; &lt;&gt;(std::istream &amp;s, Matrix&lt;temp&gt; &amp;A)
{
    ...
    return s;
}

// Operator +
template &lt;class temp&gt;
Matrix&lt;temp&gt; operator+ &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B)
{
	assert( A.itsArray2D.dim1() == B.itsArray2D.dim1() );
	assert( A.itsArray2D.dim2() == B.itsArray2D.dim2() );
	Matrix&lt;temp&gt; C( A.itsRows, A.itsColumns );
	C.itsArray2D = A.itsArray2D + B.itsArray2D;
	return C;
}

// Operator -
template &lt;class temp&gt;
Matrix&lt;temp&gt; operator- &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B)
{
	assert( A.itsArray2D.dim1() == B.itsArray2D.dim1() );
	assert( A.itsArray2D.dim2() == B.itsArray2D.dim2() );
	Matrix&lt;temp&gt; C( A.itsRows, A.itsColumns );
	C.itsArray2D = A.itsArray2D - B.itsArray2D;
	return C;

}
</code></pre>
<p>soweit funktioniert alles (d.h. ich kanns starten und die matrix mit zB int-werten füllen, zwei matrizen addieren/subtrahieren ...)</p>
<p>jetzt möcht ich halt auf die gleiche weise die elementmultiplikation bzw per funktion die richtige matrizenmultiplikation einbauen, der compiler ist aber damit anscheinend nicht einverstanden.<br />
er bringt einen fehler, welcher lautet: &quot;Syntaxfehler: Es fehlt ';' vor '&lt;'&quot; , aber nur für die operatorüberladung von * bzw die funktionsdeklaration (matrixmult).</p>
<p>so... hoffe ich hab nichts vergessen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619861</guid><dc:creator><![CDATA[Kleriker]]></dc:creator><pubDate>Mon, 24 Nov 2008 12:26:23 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 13:13:03 GMT]]></title><description><![CDATA[<p>Reihenfolgen beachten oder andere Möglichkeiten in betracht ziehen, das löst deine Probleme. Wenn wir bei deiner Methode bleiben, dann musst du es so machen:</p>
<pre><code class="language-cpp">// Predeclaration
template&lt;typename T&gt;
class Matrix;

// Template operators definition
template&lt;typename T&gt;
Matrix&lt;T&gt; operator +(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs)
{
  // ...
}

// ...

template&lt;typename T&gt;
Matrix&lt;T&gt; operator *(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs)
{
  // ...
}

// ...

template&lt;typename T&gt;
class Matrix
{
  // ...
  friend Matrix&lt;T&gt; operator + &lt;&gt;(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);
  friend Matrix&lt;T&gt; operator - &lt;&gt;(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);
  friend Matrix&lt;T&gt; operator * &lt;&gt;(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);
  friend Matrix&lt;T&gt; operator / &lt;&gt;(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);
};
</code></pre>
<p>Du kannst das aber auch anders lösen, allerdings ist der Effekt ein wenig anders, das Resultat aber dasselbe.</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
class Matrix
{
  // ...
  template&lt;typename T&gt;
  friend Matrix&lt;T&gt; operator +(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);

  template&lt;typename T&gt;
  friend Matrix&lt;T&gt; operator -(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);

  template&lt;typename T&gt;
  friend Matrix&lt;T&gt; operator *(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);

  template&lt;typename T&gt;
  friend Matrix&lt;T&gt; operator /(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs);
};

template&lt;typename T&gt;
Matrix&lt;T&gt; operator +(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs)
{
  // ...
}

// ...

template&lt;typename T&gt;
Matrix&lt;T&gt; operator *(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs)
{
  // ...
}

// ...
</code></pre>
<p>Du deklarierst deine Klasse nun als Freund von allen Templates möglichkeiten. Also eine Matrix&lt;double&gt; hat im operator + auch internen Zugriff auf eine Matrix&lt;int&gt;. Da dies aber gar nie eintritt, ist das egal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Eine letzte Möglichkeit gibt es noch, damit du die friends ganz wegbekommst:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
class Matrix
{
  // ...
  Matrix&amp; operator +=(Matrix const&amp; matrix);
  Matrix&amp; operator -=(Matrix const&amp; matrix);
  Matrix&amp; operator *=(Matrix const&amp; matrix);
  Matrix&amp; operator /=(Matrix const&amp; matrix);
};

template&lt;typename T&gt;
Matrix&lt;T&gt; operator +(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs)
{
  Matrix&lt;T&gt; copy(lhs);
  copy += rhs;
  return copy;
}

// ...

template&lt;typename T&gt;
Matrix&lt;T&gt; operator *(Matrix&lt;T&gt; const&amp; lhs, Matrix&lt;T&gt; const&amp; rhs)
{
  Matrix&lt;T&gt; copy(lhs);
  copy += rhs;
  return copy;
}

// ...
</code></pre>
<p>Ich hoffe das hilft weiter, ansonsten fragen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
<p>PS: Dem Tutorial Autor sollte man ein Feedback schreiben, dass seine Formatierungen äusserst unleserlich sind <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/1619884</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619884</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 24 Nov 2008 13:13:03 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 13:31:19 GMT]]></title><description><![CDATA[<p>Hmm.</p>
<p>Dravere lass beim ersten Beispiel mal die Spitzen klammern weg, dann gehts und dein zweites Beispiel ist Mehrdeutig.. (2 mal typename T ?!)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619892</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619892</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 24 Nov 2008 13:31:19 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 13:40:55 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<p>Dravere lass beim ersten Beispiel mal die Spitzen klammern weg, dann gehts und dein zweites Beispiel ist Mehrdeutig.. (2 mal typename T ?!)</p>
</blockquote>
<p>Nein, die spitzen Klammern müssen hin, sonst geht es eben nicht. Und das zweite Beispiel ist bei mir nicht mehrdeutig. Das zweite T überdeckt das erstere. Muss allerding gestehen, dass ich nicht weiss, ob das auch Standardkonform ist. Der MSVC schluckt es jedenfalls <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619898</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 24 Nov 2008 13:40:55 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 14:59:58 GMT]]></title><description><![CDATA[<p>so... erstmal herzlichen dank <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>also... die zweite möglichkeit hat bei mir auch funktioniert<br />
aber mit der ersten möglichkeit (per vorwärtsdekleration) haperts...<br />
da bekomm ich gleich einen fehler zum konstruktor:</p>
<blockquote>
<p>error C2039: '{ctor}': Ist kein Element von 'Matrix&lt;temp&gt;'</p>
</blockquote>
<p>was dieses {ctor} ist, weiß ich leider nicht</p>
<p>wenn ich den kon- und destruktor unter die gesamte klasse verschiebe, dann bekomm ich gleich bei der operatorüberladung von () probleme</p>
<blockquote>
<p>error C2039: '()': Ist kein Element von 'Matrix&lt;temp&gt;'</p>
</blockquote>
<p>was die dritte möglichkeit betrifft, so bilde ich mir ein (wenn ich keinen denkfehler habe), dass sie bei größeren matrizen langsamer ist, da ich erstmal die eine matrix kopieren muss</p>
<p>achja... was ist eigentlich der unterschied zwischen</p>
<pre><code class="language-cpp">template &lt;class T&gt;
</code></pre>
<p>und</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
</code></pre>
<p>außerdem konnte ich bei deiner zweiten möglichkeit sowas machen:</p>
<pre><code class="language-cpp">template&lt;class temp&gt;
class Matrix
{
  // ...
  template&lt;typename T&gt;
  friend Matrix&lt;temp&gt; operator +(Matrix&lt;temp&gt; const&amp; lhs, Matrix&lt;temp&gt; const&amp; rhs);

  template&lt;typename T&gt;
  friend Matrix&lt;temp&gt; operator -(Matrix&lt;temp&gt; const&amp; lhs, Matrix&lt;temp&gt; const&amp; rhs);

  template&lt;typename T&gt;
  friend Matrix&lt;temp&gt; operator *(Matrix&lt;temp&gt; const&amp; lhs, Matrix&lt;temp&gt; const&amp; rhs);

  template&lt;typename T&gt;
  friend Matrix&lt;temp&gt; operator /(Matrix&lt;temp&gt; const&amp; lhs, Matrix&lt;temp&gt; const&amp; rhs);
};
</code></pre>
<p>und es hat trotzdem funktioniert.... versteh ich irgendwie nicht <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/1619962</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619962</guid><dc:creator><![CDATA[Kleriker]]></dc:creator><pubDate>Mon, 24 Nov 2008 14:59:58 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 15:20:13 GMT]]></title><description><![CDATA[<p>Kleriker schrieb:</p>
<blockquote>
<p>aber mit der ersten möglichkeit (per vorwärtsdekleration) haperts...<br />
da bekomm ich gleich einen fehler zum konstruktor:</p>
<blockquote>
<p>error C2039: '{ctor}': Ist kein Element von 'Matrix&lt;temp&gt;'</p>
</blockquote>
<p>was dieses {ctor} ist, weiß ich leider nicht</p>
</blockquote>
<p>Da fehlt ein Konstruktor. Zeig doch mal die operatoren, wie du sie definiert hast, allso alles was du nun vor die Klasse hingeschrieben hast.</p>
<p>Kleriker schrieb:</p>
<blockquote>
<p>was die dritte möglichkeit betrifft, so bilde ich mir ein (wenn ich keinen denkfehler habe), dass sie bei größeren matrizen langsamer ist, da ich erstmal die eine matrix kopieren muss</p>
</blockquote>
<p>Schlaumeier, du musst so oder so eine Kopie bei einer normalen Addition oder was auch immer durchführen. Du darfst ja keine der übergebenen Parametern verändern, deshalb sind die auch konstant.</p>
<p>Kleriker schrieb:</p>
<blockquote>
<p>achja... was ist eigentlich der unterschied zwischen</p>
<pre><code class="language-cpp">template &lt;class T&gt;
</code></pre>
<p>und</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
</code></pre>
</blockquote>
<p>Das erstere ist die &quot;alte&quot; Version. Danach wurde das Schlüsselwort <code>typename</code> eingeführt. In diesem Kontext sind die beiden Varianten aber gleich. Es gibt einen Fall, wo man <code>class</code> hinschreiben muss und es mit <code>typename</code> nicht funktioniert:</p>
<pre><code class="language-cpp">template&lt;template&lt;typename T&gt; class X&gt;
</code></pre>
<p>Das sind sogenannte Template-Templates. Wird aber im allgemeinen gemieden.</p>
<p>Kleriker schrieb:</p>
<blockquote>
<p>außerdem konnte ich bei deiner zweiten möglichkeit sowas machen:</p>
<pre><code class="language-cpp">// ...
</code></pre>
<p>und es hat trotzdem funktioniert.... versteh ich irgendwie nicht <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>
</blockquote>
<p>Weiss nicht, wie Standardkonform dies ist. Hab es noch nie so gesehen und in meinem Buch hier steht auch nix davon. Kann ich also nichts dazu sagen ...</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619977</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 24 Nov 2008 15:20:13 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 15:47:06 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<p>Kleriker schrieb:</p>
<blockquote>
<p>was die dritte möglichkeit betrifft, so bilde ich mir ein (wenn ich keinen denkfehler habe), dass sie bei größeren matrizen langsamer ist, da ich erstmal die eine matrix kopieren muss</p>
</blockquote>
<p>Schlaumeier, du musst so oder so eine Kopie bei einer normalen Addition oder was auch immer durchführen. Du darfst ja keine der übergebenen Parametern verändern, deshalb sind die auch konstant.</p>
</blockquote>
<p>nein ich meine du hast einen kopiervorgang einer (möglicherweise) größeren matrix extra<br />
ansonsten würde man eine leere! matrix erstellen und die elemente einfügen, so erstellt man eine gefüllte matrix und füllt sie erneut</p>
<p>nun zu dem fehler:</p>
<pre><code class="language-cpp">#include &quot;tnt.h&quot;
#include &lt;assert.h&gt; //assert check bounces just in debug mode

template &lt;class temp&gt;
class Matrix;

// C O N S T R U C T O R  for matrixes
template&lt;class temp&gt;
Matrix&lt;temp&gt;::Matrix(unsigned long rows, unsigned long columns)
  : itsRows(rows)
  , itsColumns(columns)
  , itsArray2D(rows, columns)
{
  // object directly initialized
}

// D E S T R U C T O R  for matrixes
template&lt;class temp&gt;
Matrix&lt;temp&gt;::~Matrix(){}

//O P E R A T O R  overloading
// Operator ()
template&lt;class temp&gt;
inline temp&amp; Matrix&lt;temp&gt;::operator ()(unsigned long i, unsigned long j)
{
	assert(i &gt;= 0);
	assert(j &gt;= 0);
	assert(i &lt; itsRows);
	assert(j &lt; itsColumns);
    return itsArray2D[i][j];
}

// Operator () const
template&lt;class temp&gt;
inline const temp&amp; Matrix&lt;temp&gt;::operator()(unsigned long i, unsigned long j) const
{
	assert(i &gt;= 0);
	assert(j &gt;= 0);
	assert(i &lt; itsRows);
	assert(j &lt; itsColumns);
    return itsArray2D[i][j];
}

// Operator =
template &lt;class temp&gt;
Matrix&lt;temp&gt; &amp; Matrix&lt;temp&gt;::operator=(const temp &amp;a)
{
	itsArray2D = a;
	return *this;
}

// Template Operator &lt;&lt;
template &lt;class temp&gt;
std::ostream&amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp;s, const Matrix&lt;temp&gt; &amp;A)
{
	int M = A.itsArray2D.dim1();
	int N = A.itsArray2D.dim2();

//    s &lt;&lt; M &lt;&lt; &quot; &quot; &lt;&lt; N &lt;&lt; &quot;\n&quot;;

	for (int i=0; i&lt;M; i++)
	{
		for (int j=0; j&lt;N; j++)
		{
			s &lt;&lt; A.itsArray2D[i][j] &lt;&lt; &quot; &quot;;
		}
		s &lt;&lt; &quot;\n&quot;;
	}
    return s;
}

// Template Operator &gt;&gt;
template &lt;class temp&gt;
std::istream&amp; operator&gt;&gt; &lt;&gt;(std::istream &amp;s, Matrix&lt;temp&gt; &amp;A)
{

    int M, N;

    s &gt;&gt; M &gt;&gt; N;

	TNT::Array2D&lt;temp&gt; B(M,N);

    for (int i=0; i&lt;M; i++)
        for (int j=0; j&lt;N; j++)
        {
            s &gt;&gt;  B[i][j];
        }

	A.itsArray2D = B;
    return s;
}

// Template Operator +
template &lt;class temp&gt;
Matrix&lt;temp&gt; operator+ &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B)
{
	assert( A.itsArray2D.dim1() == B.itsArray2D.dim1() );
	assert( A.itsArray2D.dim2() == B.itsArray2D.dim2() );
	Matrix&lt;temp&gt; C( A.itsRows, A.itsColumns );
	C.itsArray2D = A.itsArray2D + B.itsArray2D;
	return C;
}

// Template Operator -
template &lt;class temp&gt;
Matrix&lt;temp&gt; operator- &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B)
{
	assert( A.itsArray2D.dim1() == B.itsArray2D.dim1() );
	assert( A.itsArray2D.dim2() == B.itsArray2D.dim2() );
	Matrix&lt;temp&gt; C( A.itsRows, A.itsColumns );
	C.itsArray2D = A.itsArray2D - B.itsArray2D;
	return C;

}

// Template Operator *
template &lt;class temp&gt;
Matrix&lt;temp&gt; operator* (const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B)
{
	assert( A.itsArray2D.dim1() == B.itsArray2D.dim1() );
	assert( A.itsArray2D.dim2() == B.itsArray2D.dim2() );
	Matrix&lt;temp&gt; C( A.itsRows, A.itsColumns );
	C.itsArray2D = A.itsArray2D * B.itsArray2D;
	return C;
}

// Template Operator /
template &lt;class temp&gt;
Matrix&lt;temp&gt; operator/ (const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B)
{
	assert( A.itsArray2D.dim1() == B.itsArray2D.dim1() );
	assert( A.itsArray2D.dim2() == B.itsArray2D.dim2() );
	Matrix&lt;temp&gt; C( A.itsRows, A.itsColumns );
	C.itsArray2D = A.itsArray2D / B.itsArray2D;
	return C;
}
</code></pre>
<p>dann kommt die klasse an sich</p>
<pre><code class="language-cpp">template &lt;class temp&gt;
class Matrix
{
private:
    unsigned long itsRows; 
    unsigned long itsColumns; 
	TNT::Array2D&lt;temp&gt; itsArray2D; // no pointer

public:
    Matrix(unsigned long rows, unsigned long columns);
    ~Matrix();

    //operator
    inline temp&amp; operator()(unsigned long i, unsigned long j);

	inline const temp&amp; operator ()(unsigned long i, unsigned long j) const;
	inline Matrix&lt;temp&gt; &amp; Matrix&lt;temp&gt;::operator=(const temp &amp;a);
	friend std::ostream&amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp;s, const Matrix&lt;temp&gt; &amp;matrix1);
	friend std::istream&amp; operator&gt;&gt; &lt;&gt;(std::istream &amp;s, Matrix&lt;temp&gt; &amp;A);
	friend Matrix&lt;temp&gt; operator+ &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B);
	friend Matrix&lt;temp&gt; operator- &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B);

	friend Matrix&lt;temp&gt; operator* &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B);
	friend Matrix&lt;temp&gt; operator/ &lt;&gt;(const Matrix&lt;temp&gt; &amp;A, const Matrix&lt;temp&gt; &amp;B);

};
</code></pre>
<p>hoffe das war jetzt nicht zu viel code <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619998</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619998</guid><dc:creator><![CDATA[Kleriker]]></dc:creator><pubDate>Mon, 24 Nov 2008 15:47:06 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 16:06:14 GMT]]></title><description><![CDATA[<p>Kleriker schrieb:</p>
<blockquote>
<p>nein ich meine du hast einen kopiervorgang einer (möglicherweise) größeren matrix extra<br />
ansonsten würde man eine leere! matrix erstellen und die elemente einfügen, so erstellt man eine gefüllte matrix und füllt sie erneut</p>
</blockquote>
<p>Überleg mal die Schritte ...<br />
Deine Version:<br />
1. Speicher reservieren<br />
2. Speicher mit leer initialisieren (also z.B. Nullen)<br />
3. Addition für jedes Element aus Matrix A und B<br />
4. Resultat speichern -&gt; zu 3. bis jedes Element durch ist.<br />
5. Matrix zurückgeben.</p>
<p>Meine Version:<br />
1. Speicher reservieren<br />
2. Speicher mit Matrix A initialisieren<br />
3. Zu jedem Element in der Matrix, das Element aus der Matrix B addieren.<br />
-&gt; wiederholen für alle Elemente<br />
4. Matrix zurückgeben.</p>
<p>Womöglich funktioniert das meinige sogar besser, da ich keinen temporären Speicher für die Addition der Elemente brauche, sondern gleich auf dem reservierten Speicher operiere. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Zu deinem Fehler:<br />
Schau doch bitte nochmals genau hin, was ich geschrieben habe. Da sind nur die freien Operatoren vorne dran. Nicht auch alle Memberfunktionen!<br />
Zudem braucht es bei der freien Funktion keine spitzen Klammern nach dem Funktionsnamen. Diese sind nur in der friend Deklaration nötig. Steht übrigens auch alles so im Tutorial, welches du liest <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1620012</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620012</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 24 Nov 2008 16:06:14 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 16:33:12 GMT]]></title><description><![CDATA[<p>hmmm... jetzt stehn nur die 2 operatoren über der klasse und es funktioniert... aber ich weiß ehrlich gesagt nicht warum. <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 />
wieso muss ich das nur bei den * und / operatoren machen?<br />
wenn ich jetzt weitere funktionen einbauen möchte, müssen diese auch drüber, oder?<br />
find das aber nicht besonders angenehm, wenn die hälfte des codes über und die andere hälfte unter der klasse ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
<p>was die spitzen klammern betrifft, in dem fall muss ich sie ja drinlassen (darum gings ja in möglichkeit eins), wenn ich sie rausnehme dann bekomm ich einen fehler (irgendwas mit main.obj).</p>
<p>ich denke ich werde die zweite möglichkeiit nutzen. wär nur nett, wenn du mir sagen kannst, warum das so ist (bei der ersten)</p>
<p>und was den kopiervorgang angeht... hast schon recht, dauert wirklich nicht länger <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1620030</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620030</guid><dc:creator><![CDATA[Kleriker]]></dc:creator><pubDate>Mon, 24 Nov 2008 16:33:12 GMT</pubDate></item><item><title><![CDATA[Reply to template operatoren&#x2F;funktionen on Mon, 24 Nov 2008 23:01:25 GMT]]></title><description><![CDATA[<p>Kleriker schrieb:</p>
<blockquote>
<p>hmmm... jetzt stehn nur die 2 operatoren über der klasse und es funktioniert... aber ich weiß ehrlich gesagt nicht warum. <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 />
wieso muss ich das nur bei den * und / operatoren machen?<br />
wenn ich jetzt weitere funktionen einbauen möchte, müssen diese auch drüber, oder?</p>
</blockquote>
<p>Alle freien template Funktionen, welche als friend deklariert werden sollen, müssen vor die Klasse hin, bei diesem Verfahren.<br />
Wieso das so ist, frag das Standardkomittee. Mir persönlich ist es ein Rätsel, aber es ist so ... punkt ^^</p>
<p>Kleriker schrieb:</p>
<blockquote>
<p>find das aber nicht besonders angenehm, wenn die hälfte des codes über und die andere hälfte unter der klasse ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
</blockquote>
<p>Deshalb hatte ich die anderen Möglichkeiten erwähnt, weil ich mir schon dachte, dass du dies nicht gut finden wirst. Wer tut das schon? <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>
<p>Kleriker schrieb:</p>
<blockquote>
<p>was die spitzen klammern betrifft, in dem fall muss ich sie ja drinlassen (darum gings ja in möglichkeit eins), wenn ich sie rausnehme dann bekomm ich einen fehler (irgendwas mit main.obj).</p>
</blockquote>
<p>1. Nein, du musst die spitzen Klammern nur bei der Frienddeklaration drin lassen. Bei der Definition der freien template Funktionen, müssen die spitzen Klammern nach dem Namen raus. Um ehrlich zu sein, verwundert es mich sogar, dass es dies nicht als Syntaxfehler angibt.<br />
2. Das &quot;irgendwas mit main.obj&quot; ist eine nicht aufgelöste Funktion, welche der Linker dir dann zuwirft, da er die Funktion nicht finden kann. Aber das ist nur dann, wenn du die spitzen Klammern bei der friend Deklaration vergisst, also die Dinger in der Klasse drin ...</p>
<p>Kleriker schrieb:</p>
<blockquote>
<p>ich denke ich werde die zweite möglichkeiit nutzen. wär nur nett, wenn du mir sagen kannst, warum das so ist (bei der ersten)</p>
</blockquote>
<p>Ist der Standard, welcher das so definiert ... die Erklärung wird dir sicher nicht reichen, wenn dem so ist, dann muss ich dich an das ISO-C++ Komittee weiterschicken <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Kleriker schrieb:</p>
<blockquote>
<p>und was den kopiervorgang angeht... hast schon recht, dauert wirklich nicht länger <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Und wieso nutzt du dann nicht gleich diese Methode? Die ist nämlich Codesparender. Du wirst nur einmal eine Addition implementieren müssen, nämlich für <code>+=</code> , bei der <code>+</code> Operation kannst du auf diesen Code zurückgreifen. Finde ich persönlich angenehmer zu programmieren, als bei der <code>+=</code> und <code>+</code> Operation Code für die Addition zu implementieren. Und das gilt natürlich immer für alle Operationen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1620235</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620235</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 24 Nov 2008 23:01:25 GMT</pubDate></item></channel></rss>