<?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 mit friend liefert &amp;quot;unresolved external symbol&amp;quot; error]]></title><description><![CDATA[<p>Hi</p>
<p>Ich habe vor eine eigene Vektorklasse zu schreiben, da sich diese gut eignet um es mit Function Overloading zu spielen. Dabei moechte ich die Klasse als Template implementieren.</p>
<p>Ich habe nun folgenden Code (in einem Header-File):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

template&lt;typename T&gt;
class Vector
{
private:
	T x;
	T y;

public:
	Vector( T a_x, T a_y ) : x(a_x), y(a_y) {}

	// Overloaded operators
	friend std::ostream &amp; operator&lt;&lt; ( const std::ostream &amp; os, const Vector&lt;T&gt; &amp; v );
};

// Overloaded operators
template&lt;typename T&gt;
std::ostream &amp; operator&lt;&lt; ( const std::ostream &amp; os, const Vector&lt;T&gt; &amp; v )
{
	os &lt;&lt; &quot;(&quot; &lt;&lt; v.x &lt;&lt; &quot;,&quot; &lt;&lt; v.y &lt;&lt; &quot;)&quot;;
	return os;
}
</code></pre>
<p>Ich bekomme allerdings folgenden Fehler (MSVS 2010):</p>
<blockquote>
<p>Error 1 error LNK2019: unresolved external symbol &quot;class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp; __cdecl operator&lt;&lt;(class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; const &amp;,class Vector&lt;int&gt; const &amp;)&quot; (??6@YAAAV?<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>b</mi><mi>a</mi><mi>s</mi><mi>i</mi><mi>c</mi><mi mathvariant="normal">_</mi><mi>o</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>m</mi><mi mathvariant="normal">@</mi><mi>D</mi><mi>U</mi><mo>?</mo></mrow><annotation encoding="application/x-tex">basic\_ostream@DU?</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:1.00444em;vertical-align:-0.31em;"></span><span class="base textstyle uncramped"><span class="mord mathit">b</span><span class="mord mathit">a</span><span class="mord mathit">s</span><span class="mord mathit">i</span><span class="mord mathit">c</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathit">o</span><span class="mord mathit">s</span><span class="mord mathit">t</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">e</span><span class="mord mathit">a</span><span class="mord mathit">m</span><span class="mord mathrm">@</span><span class="mord mathit" style="margin-right:0.02778em;">D</span><span class="mord mathit" style="margin-right:0.10903em;">U</span><span class="mclose">?</span></span></span></span>char_traits@D@std@@@std@@ABV01@ABV?$Vector@H@@@Z) referenced in function _main</p>
</blockquote>
<p>Ich nehme mal an, dass es es einen Fehler bim Linken gibt. Ich habe vorher gerade einen Artikel gelesen wie mit Templates zur Compile-Time Code generiert und gelinkt wird aber irgendwie verstehe ich das ganze noch nicht so genau.</p>
<p>Kann mir da jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/300224/template-mit-friend-liefert-quot-unresolved-external-symbol-quot-error</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 22:00:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/300224.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 28 Feb 2012 00:03:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Template mit friend liefert &amp;quot;unresolved external symbol&amp;quot; error on Tue, 28 Feb 2012 00:03:28 GMT]]></title><description><![CDATA[<p>Hi</p>
<p>Ich habe vor eine eigene Vektorklasse zu schreiben, da sich diese gut eignet um es mit Function Overloading zu spielen. Dabei moechte ich die Klasse als Template implementieren.</p>
<p>Ich habe nun folgenden Code (in einem Header-File):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

template&lt;typename T&gt;
class Vector
{
private:
	T x;
	T y;

public:
	Vector( T a_x, T a_y ) : x(a_x), y(a_y) {}

	// Overloaded operators
	friend std::ostream &amp; operator&lt;&lt; ( const std::ostream &amp; os, const Vector&lt;T&gt; &amp; v );
};

// Overloaded operators
template&lt;typename T&gt;
std::ostream &amp; operator&lt;&lt; ( const std::ostream &amp; os, const Vector&lt;T&gt; &amp; v )
{
	os &lt;&lt; &quot;(&quot; &lt;&lt; v.x &lt;&lt; &quot;,&quot; &lt;&lt; v.y &lt;&lt; &quot;)&quot;;
	return os;
}
</code></pre>
<p>Ich bekomme allerdings folgenden Fehler (MSVS 2010):</p>
<blockquote>
<p>Error 1 error LNK2019: unresolved external symbol &quot;class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp; __cdecl operator&lt;&lt;(class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; const &amp;,class Vector&lt;int&gt; const &amp;)&quot; (??6@YAAAV?<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>b</mi><mi>a</mi><mi>s</mi><mi>i</mi><mi>c</mi><mi mathvariant="normal">_</mi><mi>o</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>m</mi><mi mathvariant="normal">@</mi><mi>D</mi><mi>U</mi><mo>?</mo></mrow><annotation encoding="application/x-tex">basic\_ostream@DU?</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:1.00444em;vertical-align:-0.31em;"></span><span class="base textstyle uncramped"><span class="mord mathit">b</span><span class="mord mathit">a</span><span class="mord mathit">s</span><span class="mord mathit">i</span><span class="mord mathit">c</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathit">o</span><span class="mord mathit">s</span><span class="mord mathit">t</span><span class="mord mathit" style="margin-right:0.02778em;">r</span><span class="mord mathit">e</span><span class="mord mathit">a</span><span class="mord mathit">m</span><span class="mord mathrm">@</span><span class="mord mathit" style="margin-right:0.02778em;">D</span><span class="mord mathit" style="margin-right:0.10903em;">U</span><span class="mclose">?</span></span></span></span>char_traits@D@std@@@std@@ABV01@ABV?$Vector@H@@@Z) referenced in function _main</p>
</blockquote>
<p>Ich nehme mal an, dass es es einen Fehler bim Linken gibt. Ich habe vorher gerade einen Artikel gelesen wie mit Templates zur Compile-Time Code generiert und gelinkt wird aber irgendwie verstehe ich das ganze noch nicht so genau.</p>
<p>Kann mir da jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2186449</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2186449</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Tue, 28 Feb 2012 00:03:28 GMT</pubDate></item><item><title><![CDATA[Reply to Template mit friend liefert &amp;quot;unresolved external symbol&amp;quot; error on Tue, 28 Feb 2012 09:09:47 GMT]]></title><description><![CDATA[<p>Zwei Fehler, eine Kleinigkeit und das wonach du gefragt hast:<br />
Die Kleinigkeit zuerst: Du kannst nix in eine <strong>const</strong> ostream&amp; schreiben und aus einer const ostream&amp; bei der Rückgabe keine ostream&amp; machen. Das habe ich im folgenden stillschweigend korrigiert.</p>
<p>Das wonach du fragst:<br />
1. Warnoptionen hochdrehen. Immer. Mein Compiler sagte zum Beispiel:</p>
<pre><code>test.cc:14:85: warning: friend declaration ‘std::ostream&amp; operator&lt;&lt;(const ostream&amp;, const Vector&lt;T&gt;&amp;)’ declares a non-template function [-Wnon-template-friend]
test.cc:14:85: note: (if this is not what you intended, make sure the function template has already been declared and add &lt;&gt; after the function name here)
</code></pre>
<p>2. Und so hat der Compiler schon komplett erklärt, wie es richtig geht:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

// &quot;make sure the function template has already been declared&quot;
template&lt;typename T&gt; class Vector;
template&lt;typename T&gt; std::ostream &amp; operator&lt;&lt; (std::ostream &amp;, const Vector&lt;T&gt; &amp;);
// Ok, das haben wir hiermit gemacht.

template&lt;typename T&gt;
class Vector
{
private:
    T x;
    T y;

public:
    Vector( T a_x, T a_y ) : x(a_x), y(a_y) {}

    // &quot;and add &lt;&gt; after the function name here&quot;
    friend std::ostream &amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp; os, const Vector&lt;T&gt; &amp; v );
    // War auch nicht schwer
};

// Und schon funktioniert's
template&lt;typename T&gt;
std::ostream &amp; operator&lt;&lt; (std::ostream &amp; os, const Vector&lt;T&gt; &amp; v )
{
    os &lt;&lt; &quot;(&quot; &lt;&lt; v.x &lt;&lt; &quot;,&quot; &lt;&lt; v.y &lt;&lt; &quot;)&quot;;
    return os;
}
</code></pre>
<p>Alternativ, mit viel weniger Verrenkungen, kannst du die friend-Funktionen auch direkt in der Klasse definieren (<s>es sind dann trotzdem globale Funktionen</s><strong>edit</strong>: Genauer: Sie gehören zum Namespace der umgebenden Klasse und können nur mittels ADL gefunden werden.):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

template&lt;typename T&gt;
class Vector
{
private:
  T x;
  T y;

public:
  Vector( T a_x, T a_y ) : x(a_x), y(a_y) {}

  // ist doch viel hübscher so
  friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp; os, const Vector&lt;T&gt; &amp; v )
  {
    os &lt;&lt; &quot;(&quot; &lt;&lt; v.x &lt;&lt; &quot;,&quot; &lt;&lt; v.y &lt;&lt; &quot;)&quot;;
    return os;
  } 
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2186452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2186452</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 28 Feb 2012 09:09:47 GMT</pubDate></item><item><title><![CDATA[Reply to Template mit friend liefert &amp;quot;unresolved external symbol&amp;quot; error on Tue, 28 Feb 2012 00:25:20 GMT]]></title><description><![CDATA[<p>Danke schonmal soweit.</p>
<p>Zu 1.<br />
Ich werde mal googeln und sehen wie ich in MSVS die Warnoptionen hochdrehen kann.</p>
<p>Zu 2.<br />
Wozu braucht es das &lt;&gt; bei</p>
<pre><code class="language-cpp">friend std::ostream &amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp; os, const Vector&lt;T&gt; &amp; v );
</code></pre>
<p>Ich bin nicht mehr so fit mit Templates. Ich glaube ich muss da ein par Dinge nochmals nachlesen.</p>
<p>Zur Alternative:<br />
Jo, ist recht huebsch so. Werde ich wohl so machen <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/2186453</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2186453</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Tue, 28 Feb 2012 00:25:20 GMT</pubDate></item><item><title><![CDATA[Reply to Template mit friend liefert &amp;quot;unresolved external symbol&amp;quot; error on Tue, 28 Feb 2012 09:03:09 GMT]]></title><description><![CDATA[<p>icarus2 schrieb:</p>
<blockquote>
<p>Zu 2.<br />
Wozu braucht es das &lt;&gt; bei</p>
<pre><code class="language-cpp">friend std::ostream &amp; operator&lt;&lt; &lt;&gt;(std::ostream &amp; os, const Vector&lt;T&gt; &amp; v );
</code></pre>
<p>Ich bin nicht mehr so fit mit Templates. Ich glaube ich muss da ein par Dinge nochmals nachlesen.</p>
</blockquote>
<p>Na, das was mein Compiler da angemeckert hat:<br />
Wenn du die friend-Funktion in der Klasse so deklarierst, wie du es ursprünglich hattest, dann ist sie kein Funktionstemplate, sondern du deklarierst eine Funktion mit eben jenem Parameter, mit dem das Template instanziert wurde. Und das passt dann später nicht zu der Definition, die du als Template gemacht hast.</p>
<p>Daher muss entweder die friend-Deklaration selber auf ein Funktionstemplate verweisen. Das ist die 1. Variante und dies ist nun einmal die Syntax, wenn du den Templateparameter wiederverwenden möchtest. Du könntest auch noch einen weiteren Templateparameter innerhalb der Klasse einführen und stattdessen <code>template&lt;class T2&gt; friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp; os, const Vector&lt;T2&gt; &amp; v );</code> schreiben. Dann sind aber alle Funktionen dieser Art friends deiner speziellen Templateinstanzierung, nicht nur das spezielle mit dem gleichen Parameter. Daher willst du sicherlich das erstere, da es restriktiver ist.</p>
<p>Oder du wählst die zweite Variante und deine friend-Funktion ist kein Template mehr, aber wird dann eben instanziert, wenn das umgebende Klassentemplate instanziert wird. Und da man die Funktion mit einem Objekt der Klasse als Parameter aufruft, wird sie dann dank argument dependend look-up auch gefunden. Folgendes funktioniert beispielsweise nicht:</p>
<pre><code class="language-cpp">class Foo{};

template&lt;typename T&gt; class Vector
{
  friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp; os, const T &amp;v ) {return os;}
};

int main()
{
  Foo t;
  Vector&lt;Foo&gt; u;
  std::cout &lt;&lt; t &lt;&lt; '\n';  // Finde den Operator nicht, da ADL nicht greift
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2186485</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2186485</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 28 Feb 2012 09:03:09 GMT</pubDate></item><item><title><![CDATA[Reply to Template mit friend liefert &amp;quot;unresolved external symbol&amp;quot; error on Tue, 28 Feb 2012 10:26:20 GMT]]></title><description><![CDATA[<p>Vielen Dank fuer die ausfuehrliche Erklaerung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2186508</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2186508</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Tue, 28 Feb 2012 10:26:20 GMT</pubDate></item></channel></rss>