<?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[Probleme mit template class und operator&amp;lt;&amp;lt;]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse Matrix für welche ich gerne den operator&lt;&lt; überladen würde um diese einfach und bequem ausgeben zu können. Die Klasse sieht ungefähr so aus (nur relevante Details hier):</p>
<pre><code>#ifndef MATRIX_H
#define MATRIX_H

#include &lt;iostream&gt;
#include &lt;vector&gt;

template&lt;class T&gt; class Matrix
{
	public:
		Matrix(unsigned int rows_, unsigned int cols_);
		~Matrix();
		T&amp; getValue(unsigned int row, unsigned int col);
		unsigned int const getRows() const;
		unsigned int const getCols() const;

	private:
		std::vector&lt;T&gt; vals;
		unsigned int cols;
		unsigned int rows;
};

template &lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Matrix&lt;T&gt; const&amp; mat);

#include &quot;Matrix.inl&quot;

#endif
</code></pre>
<p>Die Implementierung meines &lt;&lt; operators sieht dann so aus:</p>
<pre><code>template &lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Matrix&lt;T&gt; const&amp; mat)
{
	for (unsigned int i = 0; i &lt; matrix.getRows(); i++){
		for (unsigned int j = 0; j &lt; matrix.getCols(); j++) {
			std::cout &lt;&lt; mat.getValue(i, j) &lt;&lt; &quot;\t&quot;;
		}
		std::cout &lt;&lt; &quot;\n&quot;;
	}
	return stream;
}
</code></pre>
<p>, die der Methode &quot;getValue&quot; so:</p>
<pre><code>template &lt;typename T&gt; T&amp; Matrix&lt;T&gt;::getValue(unsigned int row, unsigned int column)
{
	return vals[column*rows + row];
}
</code></pre>
<p>Das scheint jetzt aber nicht zu funktionieren, den wenn ich kompilieren will erhalte ich die folgende Fehlermeldung:</p>
<p><strong>error C2662: 'double &amp;Matrix&lt;double&gt;::getValue(unsigned int,unsigned int)': this-Zeiger kann nicht von 'const Matrix&lt;double&gt;' in 'Matrix&lt;double&gt; &amp;' konvertiert werden</strong></p>
<p>Das versteh ich jetzt aber nicht so ganz... was ist das Problem? Ich komm irgendwie nicht drauf</p>
<p>Viele Grüße,<br />
hs</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/323492/probleme-mit-template-class-und-operator-lt-lt</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 22:21:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323492.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 Feb 2014 17:41:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 17:42:21 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse Matrix für welche ich gerne den operator&lt;&lt; überladen würde um diese einfach und bequem ausgeben zu können. Die Klasse sieht ungefähr so aus (nur relevante Details hier):</p>
<pre><code>#ifndef MATRIX_H
#define MATRIX_H

#include &lt;iostream&gt;
#include &lt;vector&gt;

template&lt;class T&gt; class Matrix
{
	public:
		Matrix(unsigned int rows_, unsigned int cols_);
		~Matrix();
		T&amp; getValue(unsigned int row, unsigned int col);
		unsigned int const getRows() const;
		unsigned int const getCols() const;

	private:
		std::vector&lt;T&gt; vals;
		unsigned int cols;
		unsigned int rows;
};

template &lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Matrix&lt;T&gt; const&amp; mat);

#include &quot;Matrix.inl&quot;

#endif
</code></pre>
<p>Die Implementierung meines &lt;&lt; operators sieht dann so aus:</p>
<pre><code>template &lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Matrix&lt;T&gt; const&amp; mat)
{
	for (unsigned int i = 0; i &lt; matrix.getRows(); i++){
		for (unsigned int j = 0; j &lt; matrix.getCols(); j++) {
			std::cout &lt;&lt; mat.getValue(i, j) &lt;&lt; &quot;\t&quot;;
		}
		std::cout &lt;&lt; &quot;\n&quot;;
	}
	return stream;
}
</code></pre>
<p>, die der Methode &quot;getValue&quot; so:</p>
<pre><code>template &lt;typename T&gt; T&amp; Matrix&lt;T&gt;::getValue(unsigned int row, unsigned int column)
{
	return vals[column*rows + row];
}
</code></pre>
<p>Das scheint jetzt aber nicht zu funktionieren, den wenn ich kompilieren will erhalte ich die folgende Fehlermeldung:</p>
<p><strong>error C2662: 'double &amp;Matrix&lt;double&gt;::getValue(unsigned int,unsigned int)': this-Zeiger kann nicht von 'const Matrix&lt;double&gt;' in 'Matrix&lt;double&gt; &amp;' konvertiert werden</strong></p>
<p>Das versteh ich jetzt aber nicht so ganz... was ist das Problem? Ich komm irgendwie nicht drauf</p>
<p>Viele Grüße,<br />
hs</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381020</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 01 Feb 2014 17:42:21 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 17:43:13 GMT]]></title><description><![CDATA[<pre><code>template &lt;typename T&gt; T&amp; Matrix&lt;T&gt;::getValue(unsigned int row, unsigned int column)
{
    return vals[column*rows + row];
}
</code></pre>
<p>-&gt;</p>
<pre><code>template &lt;typename T&gt; T&amp; Matrix&lt;T&gt;::getValue(unsigned int row, unsigned int column) const
{
    return vals[column*rows + row];
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2381021</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381021</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 01 Feb 2014 17:43:13 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 17:47:50 GMT]]></title><description><![CDATA[<p>Hm, jetzt sagt der Compiler:</p>
<p>error C2440: 'return': 'const double' kann nicht in 'double &amp;' konvertiert werden</p>
<p>und zeigt dabei auf die getValue Funktion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381023</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381023</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 01 Feb 2014 17:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 17:50:59 GMT]]></title><description><![CDATA[<p>Ups mein Fehler.<br />
Füg noch folgende Funktion hinzu:</p>
<pre><code>template &lt;typename T&gt; const T&amp; Matrix&lt;T&gt;::getValue(unsigned int row, unsigned int column) const
{
    return vals[column*rows + row];
}
</code></pre>
<p>Dann hast du eine zum Lesen und eine zum Schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381026</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381026</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 01 Feb 2014 17:50:59 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 18:01:32 GMT]]></title><description><![CDATA[<p>Ok Danke schonmal,</p>
<p>damit kompiliert es jetzt... aber was meinst du mit <em>Füg noch folgende Funktion hinzu:</em>?</p>
<p>Die getValue Funktion gibts ja schon, also soll cih die wahrscheinlich ersetzen oder?</p>
<p>Eine weitere Frage: Ich benutze die getValue Funktion intern um den [] operator zu überladen, damit man sowas machen kann:</p>
<pre><code>Matrix&lt;double&gt; mat(3, 3);

std::cout &lt;&lt; mat[1][2];
</code></pre>
<p>Oder auch sowas:</p>
<pre><code>mat[1][2] = 3.1;
</code></pre>
<p>Funktioniert das dann überhaupt noch wenn ich die Funktion komplett konstant mache?</p>
<p>EDIT: Nach nem kurzem Test funktioniert es nicht mehr <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381027</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 01 Feb 2014 18:01:32 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 18:01:17 GMT]]></title><description><![CDATA[<p>hinzufügen != ersetzen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381029</guid><dc:creator><![CDATA[Caligulaminus]]></dc:creator><pubDate>Sat, 01 Feb 2014 18:01:17 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 18:04:43 GMT]]></title><description><![CDATA[<p>Omg, ich dachte bis jetzt immer das wenn man zwei Funktionen überladen will diese auf jeden Fall unterschiedliche Parameter haben müssen... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
<p>Cool, jetzt funktionert es! Besten Dank <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381030</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381030</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sat, 01 Feb 2014 18:04:43 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Sat, 01 Feb 2014 18:11:43 GMT]]></title><description><![CDATA[<p>happystudent schrieb:</p>
<blockquote>
<p>Omg, ich dachte bis jetzt immer das wenn man zwei Funktionen überladen will diese auf jeden Fall unterschiedliche Parameter haben müssen... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
<p>Cool, jetzt funktionert es! Besten Dank <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
</blockquote>
<p>Das const am Ende zählt auch zur Signatur.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381031</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381031</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 01 Feb 2014 18:11:43 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Mon, 03 Feb 2014 10:18:39 GMT]]></title><description><![CDATA[<ol>
<li>
<p>Endlich mal jemand, der von sich aus auf die Idee kommt, einen Wrapper für ein 2D Array um einen vector zu bauen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
</li>
<li>
<p>Ein Zugriff per [][] ist nicht ganz so einfach, üblicherweise nimmt man hier den ()-Operator. Das könnte dann so aussehen:</p>
</li>
</ol>
<pre><code>T&amp; Matrix::operator()( std::size_t Row, std::size_t Col );
const T&amp; Matrix::operator()( std::size_t Row, std::size_t Col ) const;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2381272</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381272</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Mon, 03 Feb 2014 10:18:39 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit template class und operator&amp;lt;&amp;lt; on Mon, 03 Feb 2014 10:43:57 GMT]]></title><description><![CDATA[<p>wenn du [][] willst, dann musst du dir ein proxy-objekt schreiben. sieht (in c++03) etwa so aus: (ist nun nicht direkt an deine implementierung angelehnt, dürfte dir aber etwa die idee geben)</p>
<pre><code>template&lt;typename T&gt;
class Matrix
{
	template&lt;typename MatrixPtr, typename U&gt;
	class SubscriptProxy
	{
		const std::size_t X;
		const MatrixPtr Target;

	public:
		explicit SubscriptProxy(MatrixPtr Target, std::size_t X)
			: Target(Target), X(X)
		{
		}
		U operator[] (std::size_t Y) const
		{
			return (*this-&gt;Target)(this-&gt;X, Y);
		}
	};
	// ...

public:
	// ...
	T const&amp; operator() (std::size_t X, std::size_t Y) const
	{
		// ...
	}
	T&amp; operator() (std::size_t X, std::size_t Y)
	{
		return const_cast&lt;T&amp;&gt;(static_cast&lt;Matrix const&amp;&gt;(*this)(X, Y));
	}
	SubscriptProxy&lt;Matrix const*, T const&amp;&gt; operator[] (std::size_t X) const
	{
		return SubscriptProxy&lt;Matrix const*, T const&amp;&gt;(this, X);
	}
	SubscriptProxy&lt;Matrix*, T&amp;&gt; operator[] (std::size_t X)
	{
		return SubscriptProxy&lt;Matrix*, T&amp;&gt;(this, X);
	}
};
</code></pre>
<p>ungetestet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2381278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2381278</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Mon, 03 Feb 2014 10:43:57 GMT</pubDate></item></channel></rss>