<?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[c++ class]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich mochte gerne ein class Definiren, indem ich euklidische Abstand berechne. Ich bekomme Fehlermeldungen:</p>
<p>binary 'operator -' has too many parameters,<br />
'CPoint' does not define this operator or a conversion to a type acceptable to the predefined operator,<br />
left of '.length' must have class/struct/union</p>
<p>Hier mein C++ Code:</p>
<p>class CPoint<br />
{<br />
public:<br />
float x, y;<br />
CPoint(float _x, float _y)<br />
: x(_x), y(_y) {;}</p>
<p>void set(float _x, float _y) { x = _x; y = _y; }<br />
float diff(CPoint p1, CPoint p2)<br />
{<br />
return (p2 - p1).length();<br />
}<br />
CPoint operator -(CPoint p1, CPoint p2)<br />
{<br />
return CPoint(p1.x - p2.x, p1.y - p2.y);<br />
}<br />
float length()<br />
{<br />
return sqrt(x*x+y*y);<br />
}<br />
};</p>
<p>Hoffentlich habe ich mein Problem veständlich beschrieben.<br />
Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/242791/c-class</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 19:00:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/242791.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Jun 2009 09:53:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 09:53:30 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich mochte gerne ein class Definiren, indem ich euklidische Abstand berechne. Ich bekomme Fehlermeldungen:</p>
<p>binary 'operator -' has too many parameters,<br />
'CPoint' does not define this operator or a conversion to a type acceptable to the predefined operator,<br />
left of '.length' must have class/struct/union</p>
<p>Hier mein C++ Code:</p>
<p>class CPoint<br />
{<br />
public:<br />
float x, y;<br />
CPoint(float _x, float _y)<br />
: x(_x), y(_y) {;}</p>
<p>void set(float _x, float _y) { x = _x; y = _y; }<br />
float diff(CPoint p1, CPoint p2)<br />
{<br />
return (p2 - p1).length();<br />
}<br />
CPoint operator -(CPoint p1, CPoint p2)<br />
{<br />
return CPoint(p1.x - p2.x, p1.y - p2.y);<br />
}<br />
float length()<br />
{<br />
return sqrt(x*x+y*y);<br />
}<br />
};</p>
<p>Hoffentlich habe ich mein Problem veständlich beschrieben.<br />
Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723154</guid><dc:creator><![CDATA[ns]]></dc:creator><pubDate>Mon, 08 Jun 2009 09:53:30 GMT</pubDate></item><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 10:43:44 GMT]]></title><description><![CDATA[<p>Zieh mal diff() und operator-() aus der Klasse raus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723165</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723165</guid><dc:creator><![CDATA[jencas]]></dc:creator><pubDate>Mon, 08 Jun 2009 10:43:44 GMT</pubDate></item><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 11:48:14 GMT]]></title><description><![CDATA[<p>ns schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>1. Beim nächsten mal setze bitte den Code in die C/C++-Tags<br />
2. Ich würde häufiger mit Referenzübergaben arbeiten, du kopierst hier extrem häufig deine Klasse.<br />
3. Ein paar Umbrüche würden IMHO die Lesbarkeit erhöhen.<br />
4. Wenn man den operator- überläd sollte man auch operator-= überladen. Und ersteren über zweiten außerhalb der Klasse implementieren.</p>
<p>Edit: folgendes habe ich unter VC2008 getestet:</p>
<pre><code class="language-cpp">#include &lt;cmath&gt;

class CPoint
{
  public:
    float x;
    float y;

    CPoint(float x, float y)
    :   x(x),
        y(y)
    {}

    void set(float x, float y)
    {
        this-&gt;x = x;
        this-&gt;y = y;
    }

    CPoint&amp; operator -=(CPoint const &amp; p)
    {
        this-&gt;x -= p.x;
        this-&gt;y -= p.y;
        return *this;
    }

    float length() const
    {
        return std::sqrt(x*x + y*y);
    }
};

CPoint operator-(CPoint const &amp; p1, CPoint const &amp; p2)
{
    CPoint p(p1);
    p -= p2;
    return p;
} 

float diff(CPoint const &amp; p1, CPoint const &amp; p2)
{
    return (p1 - p2).length();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1723167</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723167</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Mon, 08 Jun 2009 11:48:14 GMT</pubDate></item><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 11:12:00 GMT]]></title><description><![CDATA[<p>Ich danke euch für die Hilfe!</p>
<p>Es kommen leider noch fehler raus:</p>
<p>'const CPoint' does not define this operator or a conversion to a type acceptable to the predefined operator</p>
<p>left of '.length' must have class/struct/union Build log was saved at &quot;file://f:\EA\Debug\BuildLog.htm&quot;</p>
<p>Gruß ns</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723178</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723178</guid><dc:creator><![CDATA[ns]]></dc:creator><pubDate>Mon, 08 Jun 2009 11:12:00 GMT</pubDate></item><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 11:49:05 GMT]]></title><description><![CDATA[<p>ns schrieb:</p>
<blockquote>
<p>Ich danke euch für die Hilfe!</p>
<p>Es kommen leider noch fehler raus:</p>
</blockquote>
<p>Ich habe meinen Code mal leicht überarbeitet, und unter VC2008 getestet (s.o.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723197</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723197</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Mon, 08 Jun 2009 11:49:05 GMT</pubDate></item><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 12:03:50 GMT]]></title><description><![CDATA[<p>ich arbeite unter VS2005, konnte daran liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723208</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723208</guid><dc:creator><![CDATA[ns]]></dc:creator><pubDate>Mon, 08 Jun 2009 12:03:50 GMT</pubDate></item><item><title><![CDATA[Reply to c++ class on Mon, 08 Jun 2009 12:34:34 GMT]]></title><description><![CDATA[<p>danke euch!</p>
<p>asc, dein Code funktioniert einwandfrei. Hab meine main.cpp Datei korrigiert.<br />
Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1723231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1723231</guid><dc:creator><![CDATA[ns]]></dc:creator><pubDate>Mon, 08 Jun 2009 12:34:34 GMT</pubDate></item></channel></rss>