<?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[istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse]]></title><description><![CDATA[<p>Hallo, in meiner Belegarbeit habe ich zur Aufgabe, eine eigene Stringklasse zu programmieren. Ich komme allerdings bei dem &gt;&gt; Operator nicht ganz zurecht.</p>
<p>Hier mein Code</p>
<p>String.cpp</p>
<pre><code>CString::CString(const char *Str)
{
    this-&gt;length = strlen(Str);
    this-&gt;str = new char[length+1];
    strcpy(this-&gt;str,Str);
}

std::istream CString::&amp;operator&gt;&gt; (std::istream &amp;is, CString &amp;pStr)
{
    this-&gt;length = strlen(pStr.str);
    this-&gt;str = new char[this-&gt;length + 1];
    is &gt;&gt; this-&gt;str;
}
</code></pre>
<p>String.h</p>
<pre><code>//
#ifndef __Beleg_II__String__
#define __Beleg_II__String__

#include &lt;iostream&gt;
#include &lt;cstring&gt;

class CString
{
private:
    char *str;
    size_t length;

public:
    CString(const char *Str);
    CString();
    CString(char x, int n);

    void setChar(int idx, char x);
    int  getChar(int idx)const;
    void display()const;

    ~CString()
    {
        delete[] str;
    }

    CString operator+ (const CString &amp;pStr);
    bool operator== (const CString &amp;pStr);
    friend std::istream &amp;operator&gt;&gt; (std::ifstream &amp;is, CString &amp;pStr);
    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const CString &amp;pStr);

    char* getStr()const;
    void setStr(char *pStr);

};

#endif /* defined(__Beleg_II__String__) */
</code></pre>
<p>main.cpp</p>
<pre><code>CString Name;
cin &gt;&gt; Name;
</code></pre>
<p>Dabei bekomme ich als Fehlermedung: <strong>error: no match for 'operator&gt;&gt;' in 'std::cin &gt;&gt; Name'|</strong></p>
<p>Habe wirklich keine Ahnung woran es liegt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/327527/istream-gt-gt-operator-überladen-in-eigener-string-klasse</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Jul 2026 01:03:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327527.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 17 Aug 2014 17:44:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:44:48 GMT]]></title><description><![CDATA[<p>Hallo, in meiner Belegarbeit habe ich zur Aufgabe, eine eigene Stringklasse zu programmieren. Ich komme allerdings bei dem &gt;&gt; Operator nicht ganz zurecht.</p>
<p>Hier mein Code</p>
<p>String.cpp</p>
<pre><code>CString::CString(const char *Str)
{
    this-&gt;length = strlen(Str);
    this-&gt;str = new char[length+1];
    strcpy(this-&gt;str,Str);
}

std::istream CString::&amp;operator&gt;&gt; (std::istream &amp;is, CString &amp;pStr)
{
    this-&gt;length = strlen(pStr.str);
    this-&gt;str = new char[this-&gt;length + 1];
    is &gt;&gt; this-&gt;str;
}
</code></pre>
<p>String.h</p>
<pre><code>//
#ifndef __Beleg_II__String__
#define __Beleg_II__String__

#include &lt;iostream&gt;
#include &lt;cstring&gt;

class CString
{
private:
    char *str;
    size_t length;

public:
    CString(const char *Str);
    CString();
    CString(char x, int n);

    void setChar(int idx, char x);
    int  getChar(int idx)const;
    void display()const;

    ~CString()
    {
        delete[] str;
    }

    CString operator+ (const CString &amp;pStr);
    bool operator== (const CString &amp;pStr);
    friend std::istream &amp;operator&gt;&gt; (std::ifstream &amp;is, CString &amp;pStr);
    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const CString &amp;pStr);

    char* getStr()const;
    void setStr(char *pStr);

};

#endif /* defined(__Beleg_II__String__) */
</code></pre>
<p>main.cpp</p>
<pre><code>CString Name;
cin &gt;&gt; Name;
</code></pre>
<p>Dabei bekomme ich als Fehlermedung: <strong>error: no match for 'operator&gt;&gt;' in 'std::cin &gt;&gt; Name'|</strong></p>
<p>Habe wirklich keine Ahnung woran es liegt. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Danke <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413816</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413816</guid><dc:creator><![CDATA[Harbor]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:44:48 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:49:08 GMT]]></title><description><![CDATA[<p>hast mal ifstream statt istream geschrieb.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413817</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413817</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:49:08 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:50:51 GMT]]></title><description><![CDATA[<p>Ja, ifstream steht da. Hab falsch abgeschrieben</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413818</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413818</guid><dc:creator><![CDATA[Harbor]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:50:51 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:50:59 GMT]]></title><description><![CDATA[<p>Neben dem was volkard gesagt hat:</p>
<pre><code class="language-cpp">std::istream CString::&amp;operator&gt;&gt; (std::istream &amp;is, CString &amp;pStr)
</code></pre>
<p>Das stimmt auch nicht ganz.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413819</guid><dc:creator><![CDATA[ibfstreamp]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:50:59 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:51:08 GMT]]></title><description><![CDATA[<p>Deine Implementierung in der String.cpp ist komplett falsch. Das muss eine freie Funktion sein also ohne CString::, das bedeutet das du auch nicht auf Member zugreifen kannst.</p>
<p>Und wieso nennst du die Klasse CString, lass das C bitte weg.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413820</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413820</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:51:08 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:53:12 GMT]]></title><description><![CDATA[<p>Wo ist der <a href="http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29" rel="nofollow">Kopierkonstruktor und Zuweisungsoperator</a>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413821</guid><dc:creator><![CDATA[ro3]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:53:12 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 17:55:16 GMT]]></title><description><![CDATA[<p>Könntest du mir ein kleines Beispiel schreiben? Versteh das nicht ganz was du mit freier Funktion meinst</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413822</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413822</guid><dc:creator><![CDATA[Harbor]]></dc:creator><pubDate>Sun, 17 Aug 2014 17:55:16 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 18:05:48 GMT]]></title><description><![CDATA[<p>Harbor schrieb:</p>
<blockquote>
<p>Könntest du mir ein kleines Beispiel schreiben? Versteh das nicht ganz was du mit freier Funktion meinst</p>
</blockquote>
<pre><code>// Header
class foo
{
    int i;

public:
    ...

    // friend Deklaration
    friend std::istream&amp; operator&gt;&gt;(std::istream &amp;is, foo &amp;f);
};

// wirkliche Deklaration
std::istream&amp; operator&gt;&gt;(std::istream &amp;is, foo &amp;f);

// Implementierung
std::istream&amp; operator&gt;&gt;(std::istream &amp;is, foo &amp;f)
{
    return is &gt;&gt; f.i;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2413823</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413823</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 17 Aug 2014 18:05:48 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Sun, 17 Aug 2014 18:09:11 GMT]]></title><description><![CDATA[<p>Harbor schrieb:</p>
<blockquote>
<p>Könntest du mir ein kleines Beispiel schreiben? Versteh das nicht ganz was du mit freier Funktion meinst</p>
</blockquote>
<p>Der Operator kann keine Memberfunktion deiner Klasse sein. Denn sonst müsste das linksseitige Objekt vom Typ deiner Klasse sein, d.h. du müsstest so etwas wie</p>
<pre><code>my_string &gt;&gt; cin;
</code></pre>
<p>schreiben, um die Funktion aufzurufen. Das geht zwar, ist aber vermutlich nicht gewünscht.<br />
Du kannst aber auch keine Memberfunktionen zu std::istream hinzufügen, da du weder Zugriff auf die Klassendefinition hast und da das sowieso vom Sprachstandard her verboten ist, dass man so etwas macht.</p>
<p>Also bleibt nur eine freie Funktion, das heißt eine ganz normale Funktion, die kein Member von irgendeiner Klasse ist. Bloß, dass du sie eben operator&gt;&gt; nennst.</p>
<p>Also:</p>
<pre><code>std::istream&amp; operator&gt;&gt; (std::istream &amp;is, CString &amp;pStr);
</code></pre>
<p>Siehe auch:<br />
<a href="http://www.c-plusplus.net/forum/232010" rel="nofollow">http://www.c-plusplus.net/forum/232010</a></p>
<p>Beachte auch die anderen Antworten hier im Thread, die machen dich auf wichtige Fehler aufmerksam.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413824</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 17 Aug 2014 18:09:11 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Mon, 18 Aug 2014 10:36:21 GMT]]></title><description><![CDATA[<p>Ich habe gerade ein ähnliches Problem, von daher denke ich es ist besser es hier reinzuschreiben anstatt einen neuen Thread zu eröffnen.</p>
<pre><code>friend std::istream&amp; operator&gt;&gt;(std::istream&amp; stream, BasicString&amp; str)
	{
		stream.seekg(0, stream.end);
		int size = stream.tellg();
		stream.seekg(0, stream.beg);

		if (str.buffer != nullptr)
			delete[] str.buffer;

		str.size = size;
		str.buffer = new char_type[size + 1];
		stream.read(str.buffer, size);

		return stream;
	}
</code></pre>
<p>Ich habe auch eine eigene String-Klasse und möchte den operator&gt;&gt; überladen. Wenn ich jetzt aber sowas schreibe:</p>
<pre><code>BasicString&lt;char&gt; str;
std::cin &gt;&gt; str;
</code></pre>
<p>wird der Code einfach ausgeführt, ohne dass ich im Konsolenfenster die Chance dazu habe etwas einzugeben. Logischerweise wird dann auch nichts vom Stream gelesen.</p>
<p>Weiß da jemand weiter?</p>
<p>Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413900</guid><dc:creator><![CDATA[afhgfhgghfhfgh]]></dc:creator><pubDate>Mon, 18 Aug 2014 10:36:21 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Mon, 18 Aug 2014 10:46:22 GMT]]></title><description><![CDATA[<p>seekg und tellg funktionieren nicht mit der Konsole.<br />
Es gibt leider keine (plattforumunabhängige und afaik auch keine plattformabhängige) Möglichkeit im Voraus zu ermitteln, wie viele Zeichen eingegeben wurde.<br />
Das einzige was du machen kannst ist Zeichen für Zeichen einzulesen* und in einen Buffer zu pushen, wenn der voll ist, einen größeren anfordern und rüberkopieren.<br />
Und das ist genau das, was std::vector und push_back() machen.<br />
Nachdem alles eingelesen wurde, kannst du es dann in deinen String kopieren.</p>
<p>*Nur, wenn peek auch wirklich sagt, dass da ein Zeichen ist, ansonsten liest der für immer ein (auf der Konsole).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2413901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413901</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Mon, 18 Aug 2014 10:46:22 GMT</pubDate></item><item><title><![CDATA[Reply to istream &amp;gt;&amp;gt; Operator Überladen in eigener String Klasse on Mon, 18 Aug 2014 13:16:47 GMT]]></title><description><![CDATA[<p>Danke an alle. Jetzt funktioniert es <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>
<pre><code>std::ifstream &amp;operator&gt;&gt; (std::ifstream &amp;is, CString &amp;pStr)
{
    // Max. 128 Zeichen

    pStr.str = new char[128];
    is.getline(pStr.str, 128);

    return is;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2413930</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2413930</guid><dc:creator><![CDATA[Harbor]]></dc:creator><pubDate>Mon, 18 Aug 2014 13:16:47 GMT</pubDate></item></channel></rss>