<?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[Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung]]></title><description><![CDATA[<p>Grüsst euch,</p>
<p>ich bin gerade dabei einen Vektor selbst zu implementieren, in dem eine Zahlenfolge, repräsentiert jeweils durch eine Stelle im Vektor, sich befindet. Ein Beispiel:</p>
<p>456 + 3596<br />
(vec1) (vec2)</p>
<p>Nun gehört der plus operator überladen, welches die beiden Vektoren addiert. Es klappt alles eigentlich bis auf die Rückgabe auf vecres.</p>
<pre><code class="language-cpp">mVector vecres;

		if (43 == (int)op){ //do addition
			vecres = *vecfst + *vecsnd;
			vecres.print();

		}else		
			if (45 == (int)op){ //do subtraction
				vecres = *vecfst - *vecsnd;
			}
</code></pre>
<p>Deklaration der Überladung vom + Operator:</p>
<p>friend mVector operator+(const mVector &amp;sum, const mVector &amp;sum2);</p>
<p>Das Problem ist nun, dass vecres nach der überladung kein Daten mehr beinhaltet, da im allerletzten Schritt bei der Überladung vom + Operator (nachdem der Copy-Konstruktor geladen wurde) nochmals der Dekonstruktor auf vecres geladen wird (nachdem er bereits davor auf result (rückgabe im + operator) angewandt worden ist).</p>
<p>Ich weiß da nicht mehr weiter wo der Fehler sein könnte. Der Einfeinheithalber habe ich unter rapidshare das ganze Projekt upgeloaded. Wäre sehr dankbar, wenn mir jemand sagen könnte wie man das Problem lösen kann.</p>
<p><a href="http://rapidshare.com/files/4679240/uebung1.rar.html" rel="nofollow">http://rapidshare.com/files/4679240/uebung1.rar.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/165982/probleme-bei-rückgabe-eines-objectes-nach-binärer-operatorüberladung</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 04:22:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/165982.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 24 Nov 2006 20:15:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 20:15:41 GMT]]></title><description><![CDATA[<p>Grüsst euch,</p>
<p>ich bin gerade dabei einen Vektor selbst zu implementieren, in dem eine Zahlenfolge, repräsentiert jeweils durch eine Stelle im Vektor, sich befindet. Ein Beispiel:</p>
<p>456 + 3596<br />
(vec1) (vec2)</p>
<p>Nun gehört der plus operator überladen, welches die beiden Vektoren addiert. Es klappt alles eigentlich bis auf die Rückgabe auf vecres.</p>
<pre><code class="language-cpp">mVector vecres;

		if (43 == (int)op){ //do addition
			vecres = *vecfst + *vecsnd;
			vecres.print();

		}else		
			if (45 == (int)op){ //do subtraction
				vecres = *vecfst - *vecsnd;
			}
</code></pre>
<p>Deklaration der Überladung vom + Operator:</p>
<p>friend mVector operator+(const mVector &amp;sum, const mVector &amp;sum2);</p>
<p>Das Problem ist nun, dass vecres nach der überladung kein Daten mehr beinhaltet, da im allerletzten Schritt bei der Überladung vom + Operator (nachdem der Copy-Konstruktor geladen wurde) nochmals der Dekonstruktor auf vecres geladen wird (nachdem er bereits davor auf result (rückgabe im + operator) angewandt worden ist).</p>
<p>Ich weiß da nicht mehr weiter wo der Fehler sein könnte. Der Einfeinheithalber habe ich unter rapidshare das ganze Projekt upgeloaded. Wäre sehr dankbar, wenn mir jemand sagen könnte wie man das Problem lösen kann.</p>
<p><a href="http://rapidshare.com/files/4679240/uebung1.rar.html" rel="nofollow">http://rapidshare.com/files/4679240/uebung1.rar.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1180945</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1180945</guid><dc:creator><![CDATA[Chmielewski]]></dc:creator><pubDate>Fri, 24 Nov 2006 20:15:41 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 21:01:45 GMT]]></title><description><![CDATA[<p>Du hast den operator= nicht überladen, als Faustregel gilt, wenn du einen der folgenden Drei implementieren musst, dann musst du alle implementieren: Kopierkonstruktor, Zuweisungsoperator, Destruktor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1180970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1180970</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Fri, 24 Nov 2006 21:01:45 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 23:01:23 GMT]]></title><description><![CDATA[<p>Besten dank, soweit habs geklappt. Musste nur den = Operator überladen <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>
<p>Nur gibts mit dem dynamisch erzeugten Objekt &quot;mVector vecres&quot; Probleme. Beim verlassen der Schleife, wo dieses erzeugt wurde, wird der Dekonstruktor vom Vektor aufgerufen. Soweit verständlich. Nur dort gibts dann folgende Exception:</p>
<p>Windows has triggered a breakpoint in Übung1.exe.</p>
<p>This may be due to a corruption of the heap, and indicates a bug in Übung1.exe or any of the DLLs it has loaded.</p>
<p>The output window may have more diagnostic information</p>
<pre><code class="language-cpp">//Constructor
mVector::mVector(void)
{
	// init the vector
	m_pArray = new char[m_Alloc];
	assert(NULL != m_pArray); // abort if no memory
	m_Len = 0;
	m_Size = m_Alloc;
}
</code></pre>
<pre><code class="language-cpp">//Destructor
mVector::~mVector(void)
{
	delete[] m_pArray; // kill the vector
}
</code></pre>
<p>Kann mir ehrlich gesagt nicht vorstellen wo da ein Fehler sein sollte</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181007</guid><dc:creator><![CDATA[Chmielewski]]></dc:creator><pubDate>Fri, 24 Nov 2006 23:01:23 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 23:04:26 GMT]]></title><description><![CDATA[<p>Vermutlich ist deine Implementierung des Zuweisungs-Operators oder des Copy-Konstruktors fehlerhaft. Im Zweifelsfall mal herzeigen.</p>
<p>Im Übrigen ist das hier</p>
<pre><code class="language-cpp">assert(NULL != m_pArray); // abort if no memory
</code></pre>
<p>vollkommen sinnlos. Die new Anweisung in der Zeile davor kann keinen Null-Zeiger zurückgeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181010</guid><dc:creator><![CDATA[Z2]]></dc:creator><pubDate>Fri, 24 Nov 2006 23:04:26 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 23:24:56 GMT]]></title><description><![CDATA[<p>Diese Abfrage hat mein Prof so in seinen Sampels implementiert - müsste schon stimmen was er sagt.</p>
<pre><code class="language-cpp">//Copy-Constructor
mVector::mVector (const mVector &amp;mvec)
{
	m_pArray = new char[mvec.m_Size];
	assert (NULL != m_pArray); // abort if no memory
	memset(m_pArray,0,mvec.m_Size);

	for (int i = 0; i &lt; mvec.m_Len; i++){
		m_pArray[i] = mvec[i];}

	m_Len = mvec.m_Len;
	m_Size = mvec.m_Size;
}
</code></pre>
<pre><code class="language-cpp">mVector mVector::operator= (const mVector &amp;vec)
{
	if (this != &amp;vec)
	{
		delete (m_pArray);
		m_pArray = new char (vec.m_Size);
		assert(NULL != m_pArray); // abort if no memory
		memset(m_pArray,0,vec.m_Size);

		for (int i = 0; i &lt; vec.m_Len; i++){	
			m_pArray[i] = vec[i];}

		m_Len = vec.getLength();
		m_Size = vec.getSize();
	}
	return *this;
}
</code></pre>
<p>Kann mir nicht vorstellen, dass da ein Fehler ist. Aber ist möglich...... c++ kann brutal sein <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181014</guid><dc:creator><![CDATA[Chmielewski]]></dc:creator><pubDate>Fri, 24 Nov 2006 23:24:56 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 23:28:27 GMT]]></title><description><![CDATA[<p>Das hier:</p>
<pre><code class="language-cpp">delete (m_pArray);
</code></pre>
<p>geht so nicht. Auf new[] folgt immer ein delete[]. Das der Code nicht Exception-Sicher ist, sei mal nur am Rande erwähnt.</p>
<p>Und die Abfrage auf den Null-Zeiger ist überflüssig. Sag deinem Prof, daß er keine Ahnung hat.</p>
<p>Edit:</p>
<p>Und das geht so auch nicht:</p>
<pre><code class="language-cpp">m_pArray = new char (vec.m_Size);
</code></pre>
<p>Eckige Klammern bitte!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181015</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181015</guid><dc:creator><![CDATA[Z2]]></dc:creator><pubDate>Fri, 24 Nov 2006 23:28:27 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 23:32:19 GMT]]></title><description><![CDATA[<p>thx - jetzt läufts <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="🙂"
    /> -&gt; scheiß Klammern vertauscht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /> Und beim Lesen siehste des auch net</p>
<p>Wegen Assert werde ich mal im Inet gleich mal gucken <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/1181018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181018</guid><dc:creator><![CDATA[Chmielewski]]></dc:creator><pubDate>Fri, 24 Nov 2006 23:32:19 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Fri, 24 Nov 2006 23:45:14 GMT]]></title><description><![CDATA[<p>hmmm......</p>
<p><a href="http://en.wikipedia.org/wiki/Assertion_(computing)" rel="nofollow">http://en.wikipedia.org/wiki/Assertion_(computing)</a></p>
<blockquote>
<p>Comparison with error handling<br />
It is worth distinguishing assertions from routine error handling. Assertions should be used to document logically impossible situations — if the &quot;impossible&quot; occurs, then something fundamental is clearly wrong. This is distinct from error handling: most error conditions are possible, although some may be extremely unlikely to occur in practice. Using assertions as a general-purpose error handling mechanism is usually unwise: assertions do not allow for graceful recovery from errors, and an assertion failure will often halt the program's execution abruptly. Assertions also do not display a user-friendly error message.</p>
<p>Consider the following example of using an assertion to handle an error:</p>
<p>int *ptr = malloc(sizeof(int) * 10);<br />
assert(ptr != NULL);<br />
// use ptr<br />
<strong>Here, the programmer is aware that malloc may return a NULL pointer if memory could not be allocated.</strong> This is possible: the operating system does not guarantee that every call to malloc will succeed, and the program should be prepared to handle the failure. An assertion is probably not the best choice here, because a malloc failure is not logically impossible — it is a legitimate possibility, albeit not one that will arise very often in practice. The assertion in this example does serve one useful purpose, however: it documents that the programmer has deliberately decided not to provide robust error handling for memory allocation failures.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1181020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181020</guid><dc:creator><![CDATA[Chmielewski]]></dc:creator><pubDate>Fri, 24 Nov 2006 23:45:14 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Sat, 25 Nov 2006 00:08:08 GMT]]></title><description><![CDATA[<p><a href="https://www.google.de/search?&amp;q=bad_alloc" rel="nofollow">Google: bad_alloc</a><br />
<a href="https://www.google.de/search?&amp;q=std%3A%3Anothrow" rel="nofollow">Google: std::nothrow</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181040</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Sat, 25 Nov 2006 00:08:08 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Sat, 25 Nov 2006 00:17:29 GMT]]></title><description><![CDATA[<p>Oder zum Beispiel das hier:</p>
<p><a href="http://www.cprogramming.com/tips/showTip.php?tip=64&amp;count=30&amp;page=0" rel="nofollow">http://www.cprogramming.com/tips/showTip.php?tip=64&amp;count=30&amp;page=0</a></p>
<p>Merke: new ist nicht malloc!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181044</guid><dc:creator><![CDATA[Z2]]></dc:creator><pubDate>Sat, 25 Nov 2006 00:17:29 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Sat, 25 Nov 2006 00:18:49 GMT]]></title><description><![CDATA[<p>ich sehe es thx. Vielleicht ists bei C so, dass new 0 zurückliefert falls kein Speicher allokiert werden konnte</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181045</guid><dc:creator><![CDATA[Chmielewski]]></dc:creator><pubDate>Sat, 25 Nov 2006 00:18:49 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Sat, 25 Nov 2006 00:19:58 GMT]]></title><description><![CDATA[<p>In C gibt es kein new. Da gibt es wie gesagt malloc und das kann in der Tat 0 zurückgeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181047</guid><dc:creator><![CDATA[Z2]]></dc:creator><pubDate>Sat, 25 Nov 2006 00:19:58 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme bei Rückgabe eines Objectes nach binärer Operatorüberladung on Sat, 25 Nov 2006 09:42:40 GMT]]></title><description><![CDATA[<p>Chmielewski schrieb:</p>
<blockquote>
<p>ich sehe es thx. Vielleicht ists bei C so, dass new 0 zurückliefert falls kein Speicher allokiert werden konnte</p>
</blockquote>
<p>Dein Professor hat früher mal VS6 benutzt und von dem auf C++ geschlossen, ein sehr beliebter Irrtum.</p>
<p>Ein Tipp: Kauf dir das Buch &quot;OOP für Dummies&quot; damit du die Grundregeln für C++ kennen lernst.<br />
Ein op= implementiert man normalerweise mit dem Kopierkonstruktor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1181088</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1181088</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Sat, 25 Nov 2006 09:42:40 GMT</pubDate></item></channel></rss>