<?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[Undefined Reference auf eine abstrakte Funktion?]]></title><description><![CDATA[<p>Ich stehe momentan ziemlich auf dem Schlauch, denn ich habe zuvor bereits mit virtuellen Zuweisungsoperatoren gearbeitet, doch diesmal gelingt es mir einfach nicht... In folgendem Beispiel gibt der Compiler (gcc unter Code::Blocks) die Meldung aus, dass keine Referenz auf Array::operator= gefunden werden konnte (wie auch, ist ja abstrakt!). Der fast identisch aussehende Operator == geht komischer Weise.</p>
<pre><code>class Array
{
    public:
    virtual Array&amp; operator = ( const Array&amp; a ) = 0;
    virtual bool   operator ==( const Array&amp; a ) = 0;
};

class PolymArray : public Array
{
    public:
    Array&amp; operator = ( const Array&amp; a ) { return *this; }
    bool   operator ==( const Array&amp; a ) { return  true; }
};

int main()
{
    PolymArray a;
    PolymArray b;
    a==b;   // Kein Problem.
    a=b;     // Ein Problem!
    return 0;
}
</code></pre>
<p>Was mache ich falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/240211/undefined-reference-auf-eine-abstrakte-funktion</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 01:52:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/240211.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 03 May 2009 17:09:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Sun, 03 May 2009 17:09:58 GMT]]></title><description><![CDATA[<p>Ich stehe momentan ziemlich auf dem Schlauch, denn ich habe zuvor bereits mit virtuellen Zuweisungsoperatoren gearbeitet, doch diesmal gelingt es mir einfach nicht... In folgendem Beispiel gibt der Compiler (gcc unter Code::Blocks) die Meldung aus, dass keine Referenz auf Array::operator= gefunden werden konnte (wie auch, ist ja abstrakt!). Der fast identisch aussehende Operator == geht komischer Weise.</p>
<pre><code>class Array
{
    public:
    virtual Array&amp; operator = ( const Array&amp; a ) = 0;
    virtual bool   operator ==( const Array&amp; a ) = 0;
};

class PolymArray : public Array
{
    public:
    Array&amp; operator = ( const Array&amp; a ) { return *this; }
    bool   operator ==( const Array&amp; a ) { return  true; }
};

int main()
{
    PolymArray a;
    PolymArray b;
    a==b;   // Kein Problem.
    a=b;     // Ein Problem!
    return 0;
}
</code></pre>
<p>Was mache ich falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1704910</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704910</guid><dc:creator><![CDATA[koril-k]]></dc:creator><pubDate>Sun, 03 May 2009 17:09:58 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Sun, 03 May 2009 17:29:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class PolymArray : public Array
{
    public:
    PolymArray&amp; operator = ( const PolymArray&amp; a ) { return *this; }
    bool   operator ==( const Array&amp; a ) { return  true; }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1704915</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704915</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 03 May 2009 17:29:36 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Sun, 03 May 2009 21:31:03 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<pre><code class="language-cpp">class PolymArray : public Array
{
    public:
    PolymArray&amp; operator = ( const PolymArray&amp; a ) { return *this; }
    bool   operator ==( const Array&amp; a ) { return  true; }
};
</code></pre>
</blockquote>
<p>Ist das nicht einfach nur eine Überladung statt Polymorphie?<br />
Mal ganz nebenbei täte mich auch interessieren, warum es bei == geht und bei = nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705043</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 03 May 2009 21:31:03 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 07:32:48 GMT]]></title><description><![CDATA[<p>Richtig, drakons Version von PolymArray::operator= weist einen anderen Parameter auf als in Array::operator=. Nur beim Rückgabetyp greift das Prinzip der Kovarianz, auf das drakon wahrscheinlich hinauswollte. Wenn ich seine Version von PolymArray::operator= einsetze, wird Array::operator= also nicht redefiniert, was der Compiler auch bestätigt:</p>
<blockquote>
<p>||=== Testprogramm, Debug ===|<br />
In function <code>int main()': error: cannot declare variable \</code>a' to be of type `PolymArray'<br />
error: because the following virtual functions are abstract:<br />
error: virtual Array&amp; Array::operator=(const Array&amp;)<br />
error: cannot declare variable `b' to be of type `PolymArray'<br />
error: since type `PolymArray' has abstract virtual functions</p>
</blockquote>
<p>Gibt es noch weitere Ideen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705114</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705114</guid><dc:creator><![CDATA[koril-k]]></dc:creator><pubDate>Mon, 04 May 2009 07:32:48 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 08:02:53 GMT]]></title><description><![CDATA[<p>Ja, die Basisklasse muss den <code>operator=</code> ebenfalls implementieren. Der Aufruf von <code>operator</code> ruft alle <code>operator=</code> -Funktionen seiner Elternklassen auf. Dabei ist es ganz gleich, ob dies abstrakte Klassen sind, und die <code>operator=</code> -Funktion rein virtuell ist.<br />
Das ist analog zur nicht virtuellen <code>operator=</code> -Funktion.</p>
<pre><code class="language-cpp">//so (diese Variante ist zu bevorzugen):
class Array
{
    public:
    virtual Array&amp; operator = ( const Array&amp; a ) = 0 { return *this; }
    virtual bool   operator ==( const Array&amp; a ) = 0;
};
//oder so:
class Array
{
    public:
    virtual Array&amp; operator = ( const Array&amp; a ){ return *this; }
    virtual bool   operator ==( const Array&amp; a ) = 0;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1705127</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705127</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 04 May 2009 08:02:53 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 08:04:09 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Der Aufruf von <code>operator</code> ruft alle <code>operator=</code> -Funktionen seiner Elternklassen auf.</p>
</blockquote>
<p>Nur wenn er automatisch generiert wird. Bei selbstgeschriebenen op= muss man das falls benötigt selber machen.</p>
<p>Ich persöhnlich frage mich allerdings was ein polymorpher op= bewerkstelligen soll. Folgendes wäre z.B. sinnfrei:</p>
<pre><code class="language-cpp">class Fahrzeug;
class Auto : public Fahrzeug {/*...*/}
class Zweirad : public Fahrzeug {/*...*/}

//...

karre = mopped; //????
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1705131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705131</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 04 May 2009 08:04:09 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 10:03:40 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<p>Der Aufruf von <code>operator</code> ruft alle <code>operator=</code> -Funktionen seiner Elternklassen auf.</p>
</blockquote>
<p>Nur wenn er automatisch generiert wird. Bei selbstgeschriebenen op= muss man das falls benötigt selber machen.</p>
</blockquote>
<p>Jap. Für <code>PolymArray</code> wird auch automatisch ein Default- <code>operator=()</code> generiert. Nämlich <code>PolymArray &amp; operator=(PolymArray const &amp;)</code> . Und dieser versucht, den <code>operator=</code> von <code>Array</code> aufzurufen, was folgerichtig zu einer undefinierten Referenz führt.<br />
Erzwingt man die Benutzung des korrekten Operatos mit <code>a = static_cast&lt;Array&amp;&gt;(b)</code> , dann funktioniert es wie erwartet.<br />
Das gleiche hast Du auch beim nicht-virtuellen <code>operator=</code> :</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct test
{
    test&amp; operator=(char c){ std::cout &lt;&lt; &quot;char\n&quot;; return *this; }
};

int main()
{
    test a;
    test b;

    a = 'c'; //ruft test&amp; operator=(char c) auf

    b = a; //ruft default operator=() auf
}
</code></pre>
<p>PS: Du hast aber damit recht, dass der rein virtuelle Zuweiser wenig Sinn macht. In den seltensten Fällen tut er das, was man will.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705178</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705178</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 04 May 2009 10:03:40 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 14:31:38 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>PS: Du hast aber damit recht, dass der rein virtuelle Zuweiser wenig Sinn macht. In den seltensten Fällen tut er das, was man will.</p>
</blockquote>
<p>Das gilt aber auch für <code>operator==</code> .</p>
<p>Wenn man wirklich Objekte unterschiedlichen Typs vergleichen möchte (was ich an sich schon fragwürdig finde), wäre vielleicht etwas über <code>dynamic_cast</code> möglich. Damit mindestens <code>false</code> zurückgegeben werden kann, wenn die dynamischen Typen unterschiedlich sind.</p>
<p>Das Problem bei virtuellen Funktionen ist auch die Asymmetrie; <code>a == b</code> kann unter Umständen etwas anderes bewirken als <code>b == a</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705369</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705369</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 04 May 2009 14:31:38 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 14:52:17 GMT]]></title><description><![CDATA[<p>Danke, euer Tipp war goldrichtig! Der standardmäßig zur Verfügung gestellte Zuweisungsoperator von PolymArray bedurfte des alten, allerdings abstrakten Zuweisungsoperators.</p>
<p>Was ich mit einem abstrakten Zuweisungsoperator mache, kann ich euch gern sagen: Es handelt sich um eine abstrakte Basisklasse, die ihrerseits noch keine Elementdaten besitzt. Eine abstrakte Zuweisung ist da denkbar... zumindest jedoch nicht unsinnig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705387</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705387</guid><dc:creator><![CDATA[koril-k]]></dc:creator><pubDate>Mon, 04 May 2009 14:52:17 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 14:54:04 GMT]]></title><description><![CDATA[<p>koril-k schrieb:</p>
<blockquote>
<p>Was ich mit einem abstrakten Zuweisungsoperator mache, kann ich euch gern sagen: Es handelt sich um eine abstrakte Basisklasse, die ihrerseits noch keine Elementdaten besitzt. Eine abstrakte Zuweisung ist da denkbar... zumindest jedoch nicht unsinnig.</p>
</blockquote>
<p>Kannst Du mal ein Beispiel nennen, wieso das sinnvoll sein soll?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705391</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 04 May 2009 14:54:04 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 15:03:50 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Kannst Du mal ein Beispiel nennen, wieso das sinnvoll sein soll?</p>
</blockquote>
<p>damit kann ich endlich typen ändern. schau:</p>
<pre><code class="language-cpp">Sparkonto k1;
Girokonto k2;
k2=k1;//wird automagisch zum Sparkonto
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1705396</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705396</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 04 May 2009 15:03:50 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 15:06:25 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<p>Kannst Du mal ein Beispiel nennen, wieso das sinnvoll sein soll?</p>
</blockquote>
<p>damit kann ich endlich typen ändern. schau:</p>
<pre><code class="language-cpp">Sparkonto k1;
Girokonto k2;
k2=k1;//wird automagisch zum Sparkonto
</code></pre>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705401</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705401</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 04 May 2009 15:06:25 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 15:07:59 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>schau:</p>
</blockquote>
<p>Nettes Beispiel. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705403</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705403</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 04 May 2009 15:07:59 GMT</pubDate></item><item><title><![CDATA[Reply to Undefined Reference auf eine abstrakte Funktion? on Mon, 04 May 2009 17:23:55 GMT]]></title><description><![CDATA[<p>Meine Anwendung gebe ich euch gern. Ich habe eine abstrakte Basisklasse namens &quot;Array&quot;, jedoch ist die interne Darstellung de rDaten in den unterschiedlichen benötigten Arrays sehr verschieden. Konkret habe ich zwei Arrays: PolymArray speichert die Daten mittels Zeiger damit ich Polymorphie nutzen kann und DirecTArray speichert die Daten direkt ab wie in int a[10]. Wenn ich eine Referenz auf ein Array habe, z.B. Array&amp; a = b wobei b z.B. ein PolymArray ist, dann soll sowohl a = b als auch a = c funktionieren, wobei c ein DirectArray ist, oder noch konkreter: a = x wobei x entweder DirectArray, PolymArray oder Referenz vom Typ Array ist. Die Zuweisung übernimmt die Daten des angegebenen Arrays und legt sie in der eigenen Datenstruktur ab. Das gelingt dem Operator deshalb, weil alle Arrays über einen gemeinsamen Datenzugriff verfügen, der in der abstrakten Klasse verlangt wird und die Darstellung der Daten nach außen hin verbirgt; nämlich Array::operator[].</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705486</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705486</guid><dc:creator><![CDATA[koril-k]]></dc:creator><pubDate>Mon, 04 May 2009 17:23:55 GMT</pubDate></item></channel></rss>