<?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[Benutzerdefinierte Manipulatoren als Templates]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich wuerde mir gerne Manipulatoren definieren, um fuer meine Klassen angeben zu koennen, auf welcher Detailstufe ihre data members mittels &lt;&lt; ausgegeben werden sollen.</p>
<p>Bei der Implementation orientiere ich mich an 21.4.6.3 aus dem Stroustrup. Da ich explizite Instanzierung benutzen moechte habe ich drei Dateien (Rev_Nd ist irgendeine meiner Klassen)</p>
<p>io.h</p>
<pre><code class="language-cpp">// manipulator for setting how much details one get
template&lt;class T&gt; class Det_Manip
{
	friend ostream&amp; operator&lt;&lt;&lt;T&gt;(ostream&amp; s, const Bound_Det_Manip&lt;T&gt;&amp; bdm);
private:
	int m_level;
public:
	explicit Det_Manip(int level) : m_level(level) { }
	int level() const { return m_level; }
	Bound_Det_Manip&lt;T&gt; operator()(const T* n) const { return Bound_Det_Manip&lt;T&gt;(*this, n); }
};

// bound detail manipulator
template&lt;class T&gt; class Bound_Det_Manip
{
private:
	const Det_Manip&lt;T&gt;&amp; m_det_manip;
	const T* m_data;
public:
	Bound_Det_Manip(const Det_Manip&lt;T&gt;&amp; dm, const T* t) : m_det_manip(dm), m_data(t) { }
	const Det_Manip&lt;T&gt;&amp; det_manip() const { return m_det_manip; }
	const T* data() const { return m_data; }
};
</code></pre>
<p>io_def.h</p>
<pre><code class="language-cpp">#include &quot;standard.h&quot;

#include &quot;io.h&quot;

ostream&amp; operator&lt;&lt;(ostream&amp; s, const Bound_Det_Manip&lt;Rev_Nd&gt;&amp; bdm)
{
	... // schreib was in s

	return s;
}
</code></pre>
<p>io.cpp</p>
<pre><code class="language-cpp">#include &quot;io_def.h&quot;

template class Det_Manip&lt;Rev_Nd&gt;;
template class Bound_Det_Manip&lt;Rev_Nd&gt;;

ostream&amp; operator&lt;&lt;(ostream&amp; s, const Bound_Det_Manip&lt;Rev_Nd&gt;&amp; bdm);
</code></pre>
<p>Wenn ich nun in meiner Anwendung</p>
<pre><code class="language-cpp">ostream&amp; operator&lt;&lt;(ostream&amp; s, const Rev_Nd&amp; n)
{
	const Bound_Det_Manip&lt;Rev_Nd&gt;&amp; bdm = Det_Manip&lt;Rev_Nd&gt;(0)(&amp;n);
	s &lt;&lt; bdm;
	return s;
}
</code></pre>
<p>schreibe, erhalte ich bzgl.</p>
<pre><code class="language-cpp">s &lt;&lt; bdm;
</code></pre>
<p>die Fehlermeldung</p>
<pre><code class="language-cpp">undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; operator&lt;&lt; &lt;Rev_Nd&gt;
(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, Bound_Det_Manip&lt;Rev_Nd&gt; const&amp;)'
</code></pre>
<p>Weiss jemand, was ich hier falsch mache?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/240421/benutzerdefinierte-manipulatoren-als-templates</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 00:03:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/240421.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 06 May 2009 13:42:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Benutzerdefinierte Manipulatoren als Templates on Wed, 06 May 2009 13:42:34 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich wuerde mir gerne Manipulatoren definieren, um fuer meine Klassen angeben zu koennen, auf welcher Detailstufe ihre data members mittels &lt;&lt; ausgegeben werden sollen.</p>
<p>Bei der Implementation orientiere ich mich an 21.4.6.3 aus dem Stroustrup. Da ich explizite Instanzierung benutzen moechte habe ich drei Dateien (Rev_Nd ist irgendeine meiner Klassen)</p>
<p>io.h</p>
<pre><code class="language-cpp">// manipulator for setting how much details one get
template&lt;class T&gt; class Det_Manip
{
	friend ostream&amp; operator&lt;&lt;&lt;T&gt;(ostream&amp; s, const Bound_Det_Manip&lt;T&gt;&amp; bdm);
private:
	int m_level;
public:
	explicit Det_Manip(int level) : m_level(level) { }
	int level() const { return m_level; }
	Bound_Det_Manip&lt;T&gt; operator()(const T* n) const { return Bound_Det_Manip&lt;T&gt;(*this, n); }
};

// bound detail manipulator
template&lt;class T&gt; class Bound_Det_Manip
{
private:
	const Det_Manip&lt;T&gt;&amp; m_det_manip;
	const T* m_data;
public:
	Bound_Det_Manip(const Det_Manip&lt;T&gt;&amp; dm, const T* t) : m_det_manip(dm), m_data(t) { }
	const Det_Manip&lt;T&gt;&amp; det_manip() const { return m_det_manip; }
	const T* data() const { return m_data; }
};
</code></pre>
<p>io_def.h</p>
<pre><code class="language-cpp">#include &quot;standard.h&quot;

#include &quot;io.h&quot;

ostream&amp; operator&lt;&lt;(ostream&amp; s, const Bound_Det_Manip&lt;Rev_Nd&gt;&amp; bdm)
{
	... // schreib was in s

	return s;
}
</code></pre>
<p>io.cpp</p>
<pre><code class="language-cpp">#include &quot;io_def.h&quot;

template class Det_Manip&lt;Rev_Nd&gt;;
template class Bound_Det_Manip&lt;Rev_Nd&gt;;

ostream&amp; operator&lt;&lt;(ostream&amp; s, const Bound_Det_Manip&lt;Rev_Nd&gt;&amp; bdm);
</code></pre>
<p>Wenn ich nun in meiner Anwendung</p>
<pre><code class="language-cpp">ostream&amp; operator&lt;&lt;(ostream&amp; s, const Rev_Nd&amp; n)
{
	const Bound_Det_Manip&lt;Rev_Nd&gt;&amp; bdm = Det_Manip&lt;Rev_Nd&gt;(0)(&amp;n);
	s &lt;&lt; bdm;
	return s;
}
</code></pre>
<p>schreibe, erhalte ich bzgl.</p>
<pre><code class="language-cpp">s &lt;&lt; bdm;
</code></pre>
<p>die Fehlermeldung</p>
<pre><code class="language-cpp">undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; operator&lt;&lt; &lt;Rev_Nd&gt;
(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, Bound_Det_Manip&lt;Rev_Nd&gt; const&amp;)'
</code></pre>
<p>Weiss jemand, was ich hier falsch mache?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1706491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1706491</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Wed, 06 May 2009 13:42:34 GMT</pubDate></item><item><title><![CDATA[Reply to Benutzerdefinierte Manipulatoren als Templates on Thu, 07 May 2009 09:48:47 GMT]]></title><description><![CDATA[<p>Was soll denn die Deklaration von op&lt;&lt; in io.cpp?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1706987</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1706987</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Thu, 07 May 2009 09:48:47 GMT</pubDate></item><item><title><![CDATA[Reply to Benutzerdefinierte Manipulatoren als Templates on Thu, 07 May 2009 09:54:39 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<p>Was soll denn die Deklaration von op&lt;&lt; in io.cpp?</p>
</blockquote>
<p>Das war als explizite Instanzierung gedacht (inzwischen beschleicht mich allerdings der Verdacht, dass das alles ziemlicher Murks war, den ich da produziert habe).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1706992</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1706992</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Thu, 07 May 2009 09:54:39 GMT</pubDate></item></channel></rss>