<?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[[gelöst] Operator Überladung, unbekannte Überladung trotz definition]]></title><description><![CDATA[<p>Hallo,</p>
<p>mir ist ein merkwürdiger &quot;Fehler&quot; beim überladen von Operatoren untergekommen. Ich habe für eine Klasse den Operator + überladen. Als Beispiel hier die Headerdatei der Klasse Foo. Diese soll nur einen Integer als Variable haben. Zudem wird die Überladung Foo+int deklariert.</p>
<p>foo.h</p>
<pre><code>class Foo
{
public:
    Foo();
    int x;
};

Foo operator+(Foo &amp;lhs, int &amp;rhs);
</code></pre>
<p>In der cpp Datei wird der Konstruktor sowie die Operator-Überladung ausdefiniert. Nichts besonderes bis jetzt.<br />
foo.cpp</p>
<pre><code>Foo::Foo()
{
    this-&gt;x = 0;
}

Foo operator+(Foo &amp;lhs, int &amp;rhs)
{
    Foo newFoo;
    newFoo.x = lhs.x + rhs;
    return newFoo;
}
</code></pre>
<p>In der Main-Datei soll nun eine Instanz von Foo erstellt werden und die addition getestet werden. Wenn ich den Integer zuerst definiere (Zeile 7-9) geht die Addition auch wunderbar. Möchte ich hingegen einen &quot;vorher nicht definierten Integer&quot; addieren (Zeile 11-12) bekomme ich eine Fehlermeldung:<br />
no match for 'operator+' (operand types are 'Foo' and 'int')<br />
Foo bar2 = myFoo + 2;</p>
<p>main.cpp</p>
<pre><code>int main()
{
    Foo myFoo;
    myFoo.x = 3;
    cout &lt;&lt; &quot;myFoo.x = &quot; &lt;&lt; myFoo.x &lt;&lt; endl;

    // this will work
    int myInt = 2;
    Foo bar1 = myFoo + myInt;

    // but this won't
    Foo bar2 = myFoo + 2;

    cout &lt;&lt; &quot;bar1.x =&quot; &lt;&lt; bar1.x &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Ist dieses Verhalten so gewollt, oder habe ich einen Fehler gemacht? Was kann ich ändern damit so eine &quot;on-the-fly&quot; Addition funktioniert?</p>
<p>Ich nutze den Qt-Creator 3.0.0 (Based on Qt 5.2.0) mit absoluten Standarteinstellungen. Dieser nutzt glaube ich gcc in der Version 4.8.0 zum kompilieren.</p>
<p>Gruß,<br />
Nobody-86</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/325626/gelöst-operator-überladung-unbekannte-überladung-trotz-definition</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Jul 2026 04:09:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325626.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 May 2014 23:11:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [gelöst] Operator Überladung, unbekannte Überladung trotz definition on Sat, 10 May 2014 23:31:37 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>mir ist ein merkwürdiger &quot;Fehler&quot; beim überladen von Operatoren untergekommen. Ich habe für eine Klasse den Operator + überladen. Als Beispiel hier die Headerdatei der Klasse Foo. Diese soll nur einen Integer als Variable haben. Zudem wird die Überladung Foo+int deklariert.</p>
<p>foo.h</p>
<pre><code>class Foo
{
public:
    Foo();
    int x;
};

Foo operator+(Foo &amp;lhs, int &amp;rhs);
</code></pre>
<p>In der cpp Datei wird der Konstruktor sowie die Operator-Überladung ausdefiniert. Nichts besonderes bis jetzt.<br />
foo.cpp</p>
<pre><code>Foo::Foo()
{
    this-&gt;x = 0;
}

Foo operator+(Foo &amp;lhs, int &amp;rhs)
{
    Foo newFoo;
    newFoo.x = lhs.x + rhs;
    return newFoo;
}
</code></pre>
<p>In der Main-Datei soll nun eine Instanz von Foo erstellt werden und die addition getestet werden. Wenn ich den Integer zuerst definiere (Zeile 7-9) geht die Addition auch wunderbar. Möchte ich hingegen einen &quot;vorher nicht definierten Integer&quot; addieren (Zeile 11-12) bekomme ich eine Fehlermeldung:<br />
no match for 'operator+' (operand types are 'Foo' and 'int')<br />
Foo bar2 = myFoo + 2;</p>
<p>main.cpp</p>
<pre><code>int main()
{
    Foo myFoo;
    myFoo.x = 3;
    cout &lt;&lt; &quot;myFoo.x = &quot; &lt;&lt; myFoo.x &lt;&lt; endl;

    // this will work
    int myInt = 2;
    Foo bar1 = myFoo + myInt;

    // but this won't
    Foo bar2 = myFoo + 2;

    cout &lt;&lt; &quot;bar1.x =&quot; &lt;&lt; bar1.x &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Ist dieses Verhalten so gewollt, oder habe ich einen Fehler gemacht? Was kann ich ändern damit so eine &quot;on-the-fly&quot; Addition funktioniert?</p>
<p>Ich nutze den Qt-Creator 3.0.0 (Based on Qt 5.2.0) mit absoluten Standarteinstellungen. Dieser nutzt glaube ich gcc in der Version 4.8.0 zum kompilieren.</p>
<p>Gruß,<br />
Nobody-86</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398547</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398547</guid><dc:creator><![CDATA[Nobody-86]]></dc:creator><pubDate>Sat, 10 May 2014 23:31:37 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Operator Überladung, unbekannte Überladung trotz definition on Sat, 10 May 2014 23:17:56 GMT]]></title><description><![CDATA[<pre><code>Foo operator+(Foo const &amp;lhs, int const &amp;rhs);
</code></pre>
<pre><code>Foo operator+(Foo const &amp;lhs, int rhs);
</code></pre>
<pre><code>Foo operator+(Foo lhs, int rhs);
</code></pre>
<p>Aber von der 2 kannste keine einfache Referenz ziehen, die hat ja keinen Speicher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398551</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 10 May 2014 23:17:56 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Operator Überladung, unbekannte Überladung trotz definition on Sat, 10 May 2014 23:28:28 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>besten Dank! Ich bastel schon seit 2 Tagen daran rum und habe keine Lösung finden können. Das Minimalbeipsiel geht, mal sehen ob es auch im richtige Projekt klappt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398554</guid><dc:creator><![CDATA[Nobody-86]]></dc:creator><pubDate>Sat, 10 May 2014 23:28:28 GMT</pubDate></item></channel></rss>