<?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[cout über eine Funktion übergeben]]></title><description><![CDATA[<p>Hallo,</p>
<p>ist es möglich, die Werte, welche an cout übergeben werden<br />
sollen, über eine Funktion zu übergeben?<br />
Beispiel:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;ein Text&quot; &lt;&lt; &quot;, flags:&quot; &lt;&lt; inputFile.good() &lt;&lt; inputFile.eof() &lt;&lt; endl;
</code></pre>
<p>und dann per Funktion:</p>
<pre><code class="language-cpp">void Output_Error(??? msg)
{
	cout &lt;&lt; msg;
}
Aufruf dann irgendwie so:

Output_Error(&quot;ein Text&quot; &lt;&lt; &quot;, flags:&quot; &lt;&lt; inputFile.good() &lt;&lt; inputFile.eof() &lt;&lt; endl);
</code></pre>
<p>Ist blöd zu erklären, hoffe es versteht jemand...</p>
<p>Danke und Grüße pumi</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/213448/cout-über-eine-funktion-übergeben</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 06:26:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/213448.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 May 2008 09:57:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 09:57:52 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ist es möglich, die Werte, welche an cout übergeben werden<br />
sollen, über eine Funktion zu übergeben?<br />
Beispiel:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;ein Text&quot; &lt;&lt; &quot;, flags:&quot; &lt;&lt; inputFile.good() &lt;&lt; inputFile.eof() &lt;&lt; endl;
</code></pre>
<p>und dann per Funktion:</p>
<pre><code class="language-cpp">void Output_Error(??? msg)
{
	cout &lt;&lt; msg;
}
Aufruf dann irgendwie so:

Output_Error(&quot;ein Text&quot; &lt;&lt; &quot;, flags:&quot; &lt;&lt; inputFile.good() &lt;&lt; inputFile.eof() &lt;&lt; endl);
</code></pre>
<p>Ist blöd zu erklären, hoffe es versteht jemand...</p>
<p>Danke und Grüße pumi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511370</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511370</guid><dc:creator><![CDATA[pumi]]></dc:creator><pubDate>Sun, 18 May 2008 09:57:52 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 10:02:36 GMT]]></title><description><![CDATA[<p>Bau dir nen String und übergib den.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511374</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511374</guid><dc:creator><![CDATA[2000 Mädchen]]></dc:creator><pubDate>Sun, 18 May 2008 10:02:36 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 10:18:32 GMT]]></title><description><![CDATA[<p>2000 Mädchen schrieb:</p>
<blockquote>
<p>Bau dir nen String und übergib den.</p>
</blockquote>
<p>danke für deine Antwort.<br />
Einen String zu bauen wolte ich ja gerade vermeiden, da ja dann die<br />
ganze Power von cout verloren geht, und ich jeden Typ usw. selber<br />
casten müsste.</p>
<p>Grüsse pumi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511388</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511388</guid><dc:creator><![CDATA[pumi]]></dc:creator><pubDate>Sun, 18 May 2008 10:18:32 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 10:25:46 GMT]]></title><description><![CDATA[<p>vieleicht solltest du überlegen dir eine Klasse zu schreiben und den operator&lt;&lt; zu überladen und gucken ob dir dort die gewünschte Funktion basteln kannst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511394</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Sun, 18 May 2008 10:25:46 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 10:48:03 GMT]]></title><description><![CDATA[<p>hm.. das klingt kompliziert.<br />
Ich wüsste leider nicht wie ich so etwas hinbekommen<br />
könnte.(bin noch Anfänger)<br />
Hast du mir einen Ansatz, wie so etwas aussehen könnte?</p>
<p>Danke und Grüsse pumi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511407</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511407</guid><dc:creator><![CDATA[pumi]]></dc:creator><pubDate>Sun, 18 May 2008 10:48:03 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 11:28:50 GMT]]></title><description><![CDATA[<p>Also zunächst einmale: es geht nicht. Es gibt allerdings 2 Lösungsmöglichkeiten. Es hängt davon ab, was Du erreichen willst. Entweder implementierst Du einen std::ostream, bzw. ein std::streambuf. Das ist aber nicht unbedingt etwas für Anfänger. Oder Du verwendest Makros. Ich vermute mal, dass das Dein Problem löst. Also etwa so:</p>
<pre><code class="language-cpp">#define OutputError(msg)    do { std::cout &lt;&lt; msg; } while(false)
</code></pre>
<p>Damit funktioniert dann Dein Aufruf. Das do..while habe ich gemacht, damit es syntaktisch mit einer Anweisung identisch ist. Dann bekommst Du keine Probleme mit der Auswertreihenfolge von Operatoren oder ähnliches.<br />
In dem Makro kannst Du natürlich auch komplexere Dinge machen, wie z. B. ein Flag auswerten, ob die Ausgabe überhaupt erfolgen soll. Das hat den Vorteil, dass die ganze Formatierung nur durchgeführt wird, wenn das Flag gesetzt ist.</p>
<p>Ich habe so etwas mal selbst gemacht. Ein Beispiel findest Du unter <a href="http://www.tntnet.org/download/cxxtools-1.4.7/include/cxxtools/log/cxxtools.h" rel="nofollow">http://www.tntnet.org/download/cxxtools-1.4.7/include/cxxtools/log/cxxtools.h</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511449</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511449</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Sun, 18 May 2008 11:28:50 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 11:40:37 GMT]]></title><description><![CDATA[<p>pumi schrieb:</p>
<blockquote>
<p>2000 Mädchen schrieb:</p>
<blockquote>
<p>Bau dir nen String und übergib den.</p>
</blockquote>
<p>danke für deine Antwort.<br />
Einen String zu bauen wolte ich ja gerade vermeiden, da ja dann die<br />
ganze Power von cout verloren geht, und ich jeden Typ usw. selber<br />
casten müsste.</p>
<p>Grüsse pumi</p>
</blockquote>
<p>Is klar, Bildschirmausgabe ist natürlich viel schneller als stringstream. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /> Und warum musst du casten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511458</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511458</guid><dc:creator><![CDATA[2000 Mädchen]]></dc:creator><pubDate>Sun, 18 May 2008 11:40:37 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 12:37:04 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19201">@pumi</a>:</p>
<p>Mach dich mal über stringstreams schlau.<br />
z.B. hier <a href="http://www.cplusplus.com/reference/iostream/stringstream/" rel="nofollow">http://www.cplusplus.com/reference/iostream/stringstream/</a></p>
<p>Oder einfach mal danach googeln.</p>
<p>Alternativ kannst du auch ein ostream Objekt anlegen<br />
und das an deine Funktion übergeben um es dort auszugeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511496</guid><dc:creator><![CDATA[dark-eye]]></dc:creator><pubDate>Sun, 18 May 2008 12:37:04 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 13:31:21 GMT]]></title><description><![CDATA[<p>Hallo pumi,</p>
<p>außer den bereits erwähnten Lösungsansätzen gibt es noch die Möglichkeit, eine sogenannte Expression zu konstruieren, die dann als ganzes an die Funktion 'Output_Error' übergeben wird. Die eigentlichen Ausgabe erfolgt dann erst innerhalb der Funktion.</p>
<p>Hier mal ein Ansatz ohne Anspruch auf Optimierung und Vollständigkeit.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct Nix {};
std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, Nix )
{
    return out;
}

template&lt; typename T1, typename T2 &gt;
struct OutputExpr
{
    OutputExpr() : m_a(), m_b() {}
    OutputExpr( T1 a, T2 b )
        : m_a( a )
        , m_b( b )
    {}

    // --   für Text
    OutputExpr&lt; OutputExpr&lt;T1,T2&gt;, const char* &gt; operator&lt;&lt;( const char* x )
    {
        return OutputExpr&lt; OutputExpr&lt;T1,T2&gt;, const char* &gt;( *this, x );
    }
    // --   für endl, usw.
    OutputExpr&lt; OutputExpr&lt;T1,T2&gt;, std::ostream&amp; (*)( std::ostream&amp; ) &gt; operator&lt;&lt;( std::ostream&amp; (*x)( std::ostream&amp; ) )
    {
        return OutputExpr&lt; OutputExpr&lt;T1,T2&gt;, std::ostream&amp; (*)( std::ostream&amp; ) &gt;( *this, x );
    }
    // --   für den Rest
    template&lt; typename X &gt;
    OutputExpr&lt; OutputExpr&lt;T1,T2&gt;, X &gt; operator&lt;&lt;( const X&amp; x )
    {
        return OutputExpr&lt; OutputExpr&lt;T1,T2&gt;, X &gt;( *this, x );
    }

    friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, const OutputExpr&amp; ex )
    {
        return out &lt;&lt; ex.m_a &lt;&lt; ex.m_b;
    }
private:
    T1 m_a;
    T2 m_b;
};

// --   globale Variable
OutputExpr&lt; Nix, Nix &gt; trc;

template&lt; typename T1, typename T2 &gt;
void Output_Error( const OutputExpr&lt; T1, T2 &gt;&amp; msg )
{
    using namespace std;
    cout &lt;&lt; msg;
} 
// Makro falls man die globale Variable 'trc' nicht im Code sehen soll.
#define OUTPUT_ERROR( x ) Output_Error( trc &lt;&lt; x )

int main()
{
    using namespace std;
    ostream inputFile(0);
    Output_Error( trc &lt;&lt; &quot;ein Text&quot; &lt;&lt; &quot;, flags:&quot; &lt;&lt; inputFile.good() &lt;&lt; inputFile.eof() &lt;&lt; endl );
    OUTPUT_ERROR( &quot;Eine Ausgabe ohne 'trc' im Code&quot; &lt;&lt; endl );
    return 0;
}
</code></pre>
<p>Ist vielleicht ein bisschen heftig für einen Anfänger, aber sollte im Prinzip leicht anwendbar sein.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511541</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511541</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sun, 18 May 2008 13:31:21 GMT</pubDate></item><item><title><![CDATA[Reply to cout über eine Funktion übergeben on Sun, 18 May 2008 15:03:49 GMT]]></title><description><![CDATA[<p>vielen Dank für die Antworten.<br />
Damit sollte ich klar kommen.</p>
<p>Gruesse pumi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1511611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1511611</guid><dc:creator><![CDATA[pumi]]></dc:creator><pubDate>Sun, 18 May 2008 15:03:49 GMT</pubDate></item></channel></rss>