<?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[Operator &amp;lt;&amp;lt; überladen klappt nicht]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mir eine eigene Klasse Matrix4d geschrieben für 4-dimensionale Matrizen.</p>
<p>Nun wollte ich für die Ausgabe den Operator &quot;&lt;&lt;&quot; überladen.</p>
<p>Jedoch wird beim cout nur &quot;0x804d048&quot; ausgegeben, also scheinbar die Speicheradresse. Allerdings finde ich meinen Fehler nicht wirklich.</p>
<p>Der Code dazu:</p>
<p>Definition des Objekts:</p>
<pre><code class="language-cpp">class Matrix4d{

private:
	double** a;

public:

        double Matrix4d::getMatrix4d_value(unsigned int i, unsigned int j)

...
};

std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, const Matrix4d&amp;); //Ausgabe der Matrix
</code></pre>
<p>Um die Werte der Matrix auszugeben:</p>
<pre><code class="language-cpp">double Matrix4d::getMatrix4d_value(unsigned int i, unsigned int j) const {
	if (i &gt;= 4 || j &gt;= 4) {
		cerr &lt;&lt; &quot;Werte ausserhalb des Arrays!&quot; &lt;&lt; endl;
		exit(0);
	} else {
		return this-&gt;a[i][j];
	}
}
</code></pre>
<p>Ausgabe mit ostream:</p>
<pre><code class="language-cpp">//Ausgabefunktion
ostream&amp; operator&lt;&lt; (ostream &amp;os, const Matrix4d &amp;Ausgabe) {
	for (int i = 0; i &lt; 4; i ++) {
		os &lt;&lt; endl;
		for (int j = 0; j &lt; 4; j++) {
			os &lt;&lt; Ausgabe.getMatrix4d_value(i, j) &lt;&lt; &quot;,&quot;;
		}
	}

	return os;
}
</code></pre>
<p>Aufgerufen wird das ganze mit &quot;cout &lt;&lt; MATRIXNAME&quot;.</p>
<p>Für Hilfe bin ich sehr dankbar.</p>
<p>Gruß,</p>
<p>Tobias</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/215080/operator-lt-lt-überladen-klappt-nicht</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 20:17:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/215080.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 06 Jun 2008 09:00:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 09:02:14 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mir eine eigene Klasse Matrix4d geschrieben für 4-dimensionale Matrizen.</p>
<p>Nun wollte ich für die Ausgabe den Operator &quot;&lt;&lt;&quot; überladen.</p>
<p>Jedoch wird beim cout nur &quot;0x804d048&quot; ausgegeben, also scheinbar die Speicheradresse. Allerdings finde ich meinen Fehler nicht wirklich.</p>
<p>Der Code dazu:</p>
<p>Definition des Objekts:</p>
<pre><code class="language-cpp">class Matrix4d{

private:
	double** a;

public:

        double Matrix4d::getMatrix4d_value(unsigned int i, unsigned int j)

...
};

std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, const Matrix4d&amp;); //Ausgabe der Matrix
</code></pre>
<p>Um die Werte der Matrix auszugeben:</p>
<pre><code class="language-cpp">double Matrix4d::getMatrix4d_value(unsigned int i, unsigned int j) const {
	if (i &gt;= 4 || j &gt;= 4) {
		cerr &lt;&lt; &quot;Werte ausserhalb des Arrays!&quot; &lt;&lt; endl;
		exit(0);
	} else {
		return this-&gt;a[i][j];
	}
}
</code></pre>
<p>Ausgabe mit ostream:</p>
<pre><code class="language-cpp">//Ausgabefunktion
ostream&amp; operator&lt;&lt; (ostream &amp;os, const Matrix4d &amp;Ausgabe) {
	for (int i = 0; i &lt; 4; i ++) {
		os &lt;&lt; endl;
		for (int j = 0; j &lt; 4; j++) {
			os &lt;&lt; Ausgabe.getMatrix4d_value(i, j) &lt;&lt; &quot;,&quot;;
		}
	}

	return os;
}
</code></pre>
<p>Aufgerufen wird das ganze mit &quot;cout &lt;&lt; MATRIXNAME&quot;.</p>
<p>Für Hilfe bin ich sehr dankbar.</p>
<p>Gruß,</p>
<p>Tobias</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523895</guid><dc:creator><![CDATA[Sir_Tobias]]></dc:creator><pubDate>Fri, 06 Jun 2008 09:02:14 GMT</pubDate></item><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 09:25:33 GMT]]></title><description><![CDATA[<p>Es scheint so, als wird dein operator&lt;&lt; nicht gefunden. Ist er an der Stelle bekannt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523919</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Fri, 06 Jun 2008 09:25:33 GMT</pubDate></item><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 09:30:40 GMT]]></title><description><![CDATA[<p>bin mir grade garnicht sicher, ob dein operator da nicht ziemlich falsch liegt.</p>
<p>cout &lt;&lt; MATRIXNAME ruft imo nicht den operator&lt;&lt; deiner Matrix auf, sondern den von cout.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523925</guid><dc:creator><![CDATA[DocJunioR]]></dc:creator><pubDate>Fri, 06 Jun 2008 09:30:40 GMT</pubDate></item><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 10:05:10 GMT]]></title><description><![CDATA[<p>Wie legst Du die Matrix denn an?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523939</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Fri, 06 Jun 2008 10:05:10 GMT</pubDate></item><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 10:34:24 GMT]]></title><description><![CDATA[<blockquote>
<p>Jedoch wird beim cout nur &quot;0x804d048&quot; ausgegeben, also scheinbar die Speicheradresse.</p>
</blockquote>
<p>Das ist nicht nur scheinbar die Speicheradresse, das ist die Adresse deines Matrixobjektes.</p>
<p>Pseudo:</p>
<pre><code class="language-cpp">Matrix* myMatrix = new MyMatrix( ... );
cout &lt;&lt; myMatrix &lt;&lt; endl;
delete myMatrix;
</code></pre>
<p>Hier wird die Adresse ausgegeben, da es sich um einen Pointer handelt. Entweder du definierst einen Stream-Operator für Matrix-Pointer (nicht sehr sinnvoll) oder du dereferenzierst den Pointer</p>
<p>Pseudo:</p>
<pre><code class="language-cpp">Matrix* myMatrix = new MyMatrix( ... );
cout &lt;&lt; *myMatrix &lt;&lt; endl;
delete myMatrix;
</code></pre>
<p>oder verwendest direkt &quot;normale&quot; Objekte</p>
<p>Pseudo:</p>
<pre><code class="language-cpp">Matrix myMatrix( ... );
cout &lt;&lt; myMatrix &lt;&lt; endl;
</code></pre>
<p>Grüße... Heiko</p>
<p>EDIT:</p>
<blockquote>
<pre><code class="language-cpp">double Matrix4d::getMatrix4d_value(unsigned int i, unsigned int j) const;
</code></pre>
</blockquote>
<p>Die Methode solltest du umbenennen. Den Klassennamen in der Methode zu wiederholen ist sinnfrei, da die Methode ohnehin an die Klasse gebunden ist. _value ist m.E. geschmacksache (was soll man sonst aus einer Matrix herausholen außer Werte?)</p>
<pre><code class="language-cpp">double Matrix4d::get(size_t i, size_t j) const;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1523949</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523949</guid><dc:creator><![CDATA[bwbg]]></dc:creator><pubDate>Fri, 06 Jun 2008 10:34:24 GMT</pubDate></item><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 10:41:50 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>vielen Dank schon mal für die schnellen Antworten.</p>
<p>Die Matrix lege ich mit einem Copy-Konstruktor an, der Werte aus einem String ausliest und sie in double-Werte umwandelt:</p>
<pre><code class="language-cpp">string matrixwerte = &quot;(1,2,3,4)(5,6,7,8)(9,10,11,12)(13,14,15,16)&quot;;
 Matrix4d* matrixtest = new Matrix4d(matrixwerte);
</code></pre>
<p>Die Klasse ist folgendermaßen in der Matrix4d.h definiert:</p>
<pre><code class="language-cpp">class Matrix4d{

private:
	double** a;

public:
	Matrix4d();  //Default Konstruktor
	Matrix4d(const string); //Copy-Konstruktor soll Double-Werte einlesen

	bool operator== (const Matrix4d &amp;) const; //Vergleich mit anderer Matrix
	Matrix4d operator= (const Matrix4d &amp;); //Kopieren der Matrix-Werte
	Matrix4d operator+ (const Matrix4d &amp;) const; //Addition zweier Matrizen
	Matrix4d operator* (const Matrix4d &amp;) const; //Matrizenmultiplikation
	Matrix4d operator* (const double) const; //Matrix mit Skalar mult.
	Vector3d operator* (const Vector3d &amp;) const; //Mult mit Vektor
	Matrix4d getMatrix4d() const; //get the Matrix
	double getMatrix4d_value(unsigned int i, unsigned int j) const; 
	void matrix4dwert(int i, int j, double value); //Wertzuweisung

};

std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, const Matrix4d&amp;); //Ausgabe der Matrix
</code></pre>
<p>Die Matrix wird dann folgendermaßen angelegt:</p>
<pre><code class="language-cpp">//Copy-Konstruktor
Matrix4d::Matrix4d(const string Einlesen) {

	this-&gt;a = new double*[4];

	//Matrix erstellen
	for (int i = 0; i &lt; 4; i++) {
		this-&gt;a[i] = new double[4];
	}

	//Matrix mit Nullen fuellen
	for (int i = 0; i &lt; 4; i++) {
		for (int j = 0; j &lt; 4; j++) {
			this-&gt;a[i][j] = 0;
		}
	}

	//Matrix mit lustigen Werten fuellen
	int k = 0;
	int l = 0;
	string S = &quot;&quot;;
	for (unsigned int i = 1; i &lt; Einlesen.length(); i++) {
		if (Einlesen[i] != ',' &amp;&amp; Einlesen[i] != '(' &amp;&amp; Einlesen[i] != ')') {
			S += Einlesen[i];
		} 
		else if (Einlesen[i] == ')') {
			this-&gt;a[k][l] = atof(S.data());
			k++;
			S = &quot;&quot;;
			l = 0;
		}
		else if (Einlesen[i] == ',') {
			this-&gt;a[k][l] = atof(S.data());
			S = &quot;&quot;;
			l++;
		} 
	}
}
</code></pre>
<p>Und hier noch operator=, was wohl auch benutzt wird bei der &quot;=&quot;-Zuweisung?</p>
<pre><code class="language-cpp">//Kopieren der Matrix-Werte
Matrix4d Matrix4d::operator= (const Matrix4d&amp; mat2){ 

	for(int i = 0; i &lt; 4; i++){
		for(int j = 0; j &lt; 4; j++) {
			this-&gt;a[i][j] = mat2.a[i][j];
		}
	}
	Matrix4d test;

	for(int i = 0; i &lt; 4; i++){
		for(int j = 0; j &lt; 4; j++) {
			test.a[i][j] = mat2.a[i][j];
		}
	}

	return test;
}
</code></pre>
<p>Das mit dem Erstellen der Matrix klappt soweit auch.</p>
<p>Wenn ich mit der Methode</p>
<pre><code class="language-cpp">getMatrix4d_value(unsigned int i, unsigned int j)
</code></pre>
<p>den Wert direkt ausgeben lasse, also</p>
<pre><code class="language-cpp">matrixtest-&gt;getMatrix4d_value(...)
</code></pre>
<p>kommt auch der Wert als Zahl zurück.</p>
<p>Nur mit dem Operator &lt;&lt; klappt es nicht, da kommt dieser Hex-Wert von oben...</p>
<p>Gruß,</p>
<p>Tobias</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523957</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523957</guid><dc:creator><![CDATA[Sir_Tobias]]></dc:creator><pubDate>Fri, 06 Jun 2008 10:41:50 GMT</pubDate></item><item><title><![CDATA[Reply to Operator &amp;lt;&amp;lt; überladen klappt nicht on Fri, 06 Jun 2008 10:47:37 GMT]]></title><description><![CDATA[<blockquote>
<p>Hier wird die Adresse ausgegeben, da es sich um einen Pointer handelt. Entweder du definierst einen Stream-Operator für Matrix-Pointer (nicht sehr sinnvoll) oder du dereferenzierst den Pointer</p>
<p>Pseudo:<br />
C/C++ Code:<br />
Matrix* myMatrix = new MyMatrix( ... );<br />
cout &lt;&lt; <em>myMatrix &lt;&lt; endl;<br />
delete myMatrix;<br />
C/C++ Code:<br />
Matrix</em> myMatrix = new MyMatrix( ... );<br />
cout &lt;&lt; <em>myMatrix &lt;&lt; endl;<br />
delete myMatrix;<br />
C/C++ Code:<br />
Matrix</em> myMatrix = new MyMatrix( ... );<br />
cout &lt;&lt; *myMatrix &lt;&lt; endl;<br />
delete myMatrix;</p>
</blockquote>
<p>Vielen Dank. Das war genau mein Fehler. Ich habe nicht daran gedacht, den Pointer zu dereferenzieren.</p>
<p>Vielen Dank auch den anderen für die Hilfe.</p>
<p>Gruß,</p>
<p>Tobias</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1523961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1523961</guid><dc:creator><![CDATA[Sir_Tobias]]></dc:creator><pubDate>Fri, 06 Jun 2008 10:47:37 GMT</pubDate></item></channel></rss>