<?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[Include, Templates, operator&amp;lt;&amp;lt;]]></title><description><![CDATA[<p>Ich bin gerade dabei c++ zu lernen und habe folgendes Problem:</p>
<p>Ich hab drei Dateien:<br />
LinearAlgebra.cpp<br />
matrix.cpp<br />
matrix.h</p>
<p>Ich möchte den operator&lt;&lt; für Matrizen definieren:<br />
Dazu steht in der matrix.h unter anderem:</p>
<pre><code class="language-cpp">// [...]
template&lt;class T&gt;
class Matrix {
public:
    // [...]
    friend std::ostream&amp; operator&lt;&lt; &lt;T&gt;(std::ostream&amp;, const Matrix &lt;T&gt; &amp;)
    // [...]
</code></pre>
<p>In der matrix.cpp implementiere ich die Funktion:</p>
<pre><code class="language-cpp">template &lt;class T&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream &amp;strm, const Matrix&lt;T&gt; &amp;m) {
	strm &lt;&lt; &quot;&quot; &lt;&lt; m.dim1_ &lt;&lt; &quot; x &quot; &lt;&lt; m.dim2_ &lt;&lt; &quot; :&quot; &lt;&lt; std::endl;
	for (int i = 0; i &lt; m.dim1_; i++) {
		strm &lt;&lt; &quot;  [&quot;;
		for (int j = 0; j &lt; m.dim2_; j++) {
			if (j != 0) strm &lt;&lt; &quot;, &quot;;
			strm &lt;&lt; m.getEntry(i, j);
		}
		strm &lt;&lt; &quot;]&quot; &lt;&lt; std::endl;
	}
	return strm;
}
</code></pre>
<p>Wenn ich der LinearAlgebra.cpp (in der ich den operator&lt;&lt; verwende)</p>
<pre><code class="language-cpp">#include &quot;matrix.h&quot;
</code></pre>
<p>schreibe, bekomme ich ziemlich viele (gleiche Fehler):</p>
<blockquote>
<p>C:/Program Files/cpp-eclipse/Projekte/Linear Algebra/Debug/../src/Linear Algebra.cpp:60: undefined reference to `std::ostream&amp; operator&lt;&lt; &lt;int&gt;(std::ostream&amp;, Matrix&lt;int&gt; const&amp;)'</p>
</blockquote>
<p>oder auch:</p>
<blockquote>
<p>C:/Program Files/cpp-eclipse/Projekte/Linear Algebra/Debug/../src/Linear Algebra.cpp:221: undefined reference to `std::ostream&amp; operator&lt;&lt; &lt;double&gt;(std::ostream&amp;, Matrix&lt;double&gt; const&amp;)'</p>
</blockquote>
<p>Wenn ich in der LinearAlgebra.cpp nicht die matrix.h, sondern die matrix.cpp include, wird alles fehlerfrei kompiliert.</p>
<p>Ich hab schon versucht, sowas wie</p>
<pre><code class="language-cpp">template &lt;class T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream &amp;strm, const Matrix&lt;T&gt; &amp;m);
</code></pre>
<p>in die matrix.h hinzuzufügen, allerdings zeigte das auch keine Wirkung.</p>
<p>Da der gesamte Quellcode insgesamt ca. 1000 Zeilen hat, möchte ich ihn hier nicht komplett posten. Wenn ich noch irgendwas fehlt, was ihr braucht, um mir sagen zu können, was ich falsch gemacht habe, fragt einfach nach.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/297624/include-templates-operator-lt-lt</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 10:08:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/297624.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 03 Jan 2012 15:06:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Include, Templates, operator&amp;lt;&amp;lt; on Tue, 03 Jan 2012 15:06:26 GMT]]></title><description><![CDATA[<p>Ich bin gerade dabei c++ zu lernen und habe folgendes Problem:</p>
<p>Ich hab drei Dateien:<br />
LinearAlgebra.cpp<br />
matrix.cpp<br />
matrix.h</p>
<p>Ich möchte den operator&lt;&lt; für Matrizen definieren:<br />
Dazu steht in der matrix.h unter anderem:</p>
<pre><code class="language-cpp">// [...]
template&lt;class T&gt;
class Matrix {
public:
    // [...]
    friend std::ostream&amp; operator&lt;&lt; &lt;T&gt;(std::ostream&amp;, const Matrix &lt;T&gt; &amp;)
    // [...]
</code></pre>
<p>In der matrix.cpp implementiere ich die Funktion:</p>
<pre><code class="language-cpp">template &lt;class T&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream &amp;strm, const Matrix&lt;T&gt; &amp;m) {
	strm &lt;&lt; &quot;&quot; &lt;&lt; m.dim1_ &lt;&lt; &quot; x &quot; &lt;&lt; m.dim2_ &lt;&lt; &quot; :&quot; &lt;&lt; std::endl;
	for (int i = 0; i &lt; m.dim1_; i++) {
		strm &lt;&lt; &quot;  [&quot;;
		for (int j = 0; j &lt; m.dim2_; j++) {
			if (j != 0) strm &lt;&lt; &quot;, &quot;;
			strm &lt;&lt; m.getEntry(i, j);
		}
		strm &lt;&lt; &quot;]&quot; &lt;&lt; std::endl;
	}
	return strm;
}
</code></pre>
<p>Wenn ich der LinearAlgebra.cpp (in der ich den operator&lt;&lt; verwende)</p>
<pre><code class="language-cpp">#include &quot;matrix.h&quot;
</code></pre>
<p>schreibe, bekomme ich ziemlich viele (gleiche Fehler):</p>
<blockquote>
<p>C:/Program Files/cpp-eclipse/Projekte/Linear Algebra/Debug/../src/Linear Algebra.cpp:60: undefined reference to `std::ostream&amp; operator&lt;&lt; &lt;int&gt;(std::ostream&amp;, Matrix&lt;int&gt; const&amp;)'</p>
</blockquote>
<p>oder auch:</p>
<blockquote>
<p>C:/Program Files/cpp-eclipse/Projekte/Linear Algebra/Debug/../src/Linear Algebra.cpp:221: undefined reference to `std::ostream&amp; operator&lt;&lt; &lt;double&gt;(std::ostream&amp;, Matrix&lt;double&gt; const&amp;)'</p>
</blockquote>
<p>Wenn ich in der LinearAlgebra.cpp nicht die matrix.h, sondern die matrix.cpp include, wird alles fehlerfrei kompiliert.</p>
<p>Ich hab schon versucht, sowas wie</p>
<pre><code class="language-cpp">template &lt;class T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream &amp;strm, const Matrix&lt;T&gt; &amp;m);
</code></pre>
<p>in die matrix.h hinzuzufügen, allerdings zeigte das auch keine Wirkung.</p>
<p>Da der gesamte Quellcode insgesamt ca. 1000 Zeilen hat, möchte ich ihn hier nicht komplett posten. Wenn ich noch irgendwas fehlt, was ihr braucht, um mir sagen zu können, was ich falsch gemacht habe, fragt einfach nach.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163296</guid><dc:creator><![CDATA[Gast_31415]]></dc:creator><pubDate>Tue, 03 Jan 2012 15:06:26 GMT</pubDate></item><item><title><![CDATA[Reply to Include, Templates, operator&amp;lt;&amp;lt; on Tue, 03 Jan 2012 15:15:37 GMT]]></title><description><![CDATA[<p>Templates müssen in der Headerdatei implementiert werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163304</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163304</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Tue, 03 Jan 2012 15:15:37 GMT</pubDate></item><item><title><![CDATA[Reply to Include, Templates, operator&amp;lt;&amp;lt; on Tue, 03 Jan 2012 15:27:29 GMT]]></title><description><![CDATA[<p>Heißt es nicht immer, dass man in die Headerdatei keinen Code schreibt? Oder gibt es bei Templates keine Alternative?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163310</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163310</guid><dc:creator><![CDATA[Gast_31415]]></dc:creator><pubDate>Tue, 03 Jan 2012 15:27:29 GMT</pubDate></item><item><title><![CDATA[Reply to Include, Templates, operator&amp;lt;&amp;lt; on Tue, 03 Jan 2012 15:30:28 GMT]]></title><description><![CDATA[<p>Gast_31415 schrieb:</p>
<blockquote>
<p>Heißt es nicht immer, dass man in die Headerdatei keinen Code schreibt?</p>
</blockquote>
<p>Nein, heißt es nie. Das ist eine ziemlich ignorante Verallgemeinerung eines Tips, wie man Compilezeiten verringern und Abhängigkeiten reduzieren kann.</p>
<p>[qote]Oder gibt es bei Templates keine Alternative?[/quote] Nein, gibt es nicht. (Bevor jetzt ein Besserwisser was von &quot;export&quot; faselt: ist auch keine Alternative, weil nicht portabel und veraltet)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163314</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163314</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 03 Jan 2012 15:30:28 GMT</pubDate></item><item><title><![CDATA[Reply to Include, Templates, operator&amp;lt;&amp;lt; on Tue, 03 Jan 2012 15:55:45 GMT]]></title><description><![CDATA[<p>Ich hab noch eine andere Frage zu templates:</p>
<p>In der matrix.cpp hab ich am Ende folgendes stehen:</p>
<pre><code class="language-cpp">template class Matrix&lt;int&gt;;
template class Matrix&lt;long&gt;;
template class Matrix&lt;float&gt;;
template class Matrix&lt;double&gt;;
</code></pre>
<p>Ich finde es unschön, dass man sich dort auf erlaubten Typen festlegen muss.</p>
<p>Wenn ich das obige weglasse, bekomme ich in der LinearAlgebra.cpp viele Fehler vom Typ &quot;undefined reference to&quot;. Wenn ich statt der matrix.h die matrix.cpp include, wird fehlerfrei kompliliert.</p>
<p>Anscheind muss man sich also nicht unbedingt auf die erlaubten Klassen festlegen. Allerdings möchte ich dafür nicht die .cpp-Datei includen. Gibt es da eine Lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163327</guid><dc:creator><![CDATA[Gast_31415]]></dc:creator><pubDate>Tue, 03 Jan 2012 15:55:45 GMT</pubDate></item><item><title><![CDATA[Reply to Include, Templates, operator&amp;lt;&amp;lt; on Tue, 03 Jan 2012 16:06:44 GMT]]></title><description><![CDATA[<p>Dies instanziert die Templates explizit, so dass diese Methoden dieser Klasse existieren und hinterher mit dem Code aus der linearAlgebra.cpp gelinkt werden können, obwohl das Template dort nie instanziert wird (vermutlich wird dort eine Forward-Declaration benutzt). Dies ist dann auch ein Fall, wo die Implementierung der Templates in der linearAlgebra.cpp nicht vorliegen muss.</p>
<p>Wenn du die Templates an anderer Stelle für andere Typen benutzt, wirst du sie in der Regel auch instanzieren (d.h. es wird Code für die Methoden erzeugt und übersetzt). Dafür muss dann aber eine Implementierung der Methoden vorliegen.</p>
<p>P.S.: Das nennt man übrigens <em>Explicit Instantiation</em>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2163334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2163334</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 03 Jan 2012 16:06:44 GMT</pubDate></item></channel></rss>