<?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[Konstruktor Problem]]></title><description><![CDATA[<p>hallo. ich habe eine Klasse erstellt mit 2 überladenden Konstruktoren:</p>
<pre><code class="language-cpp">class rectangle
{
private:
    int itsWidth;
    int itsLenght;
public:
    rectangle(); //HIER!
    rectangle(int widht,int lenght); //Hier!
    ~rectangle();
    int getWidth();
    int getLenght();

};
</code></pre>
<p>Konstrukoren wurden so initalisiert:</p>
<pre><code class="language-cpp">rectangle::rectangle()
{
    itsWidth=5;
    itsWidth=10;
}
rectangle::rectangle(int widht, int lenght)
{
    itsWidth=widht;
    itsLenght=lenght;

}
</code></pre>
<p>Wenn ich dem 2. Konstruktor nun Werte zuweisen möchte, klappt dies nicht:</p>
<pre><code class="language-cpp">rectangle rechteck1, rechteck2;
  rechteck2.rectangle(10,20);
</code></pre>
<p>main.cpp:25: error: invalid use of ‘class rectangle’</p>
<p>Hoffentlich kann mir einer helfen, danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/269036/konstruktor-problem</link><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 11:06:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/269036.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Jun 2010 19:39:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 19:39:08 GMT]]></title><description><![CDATA[<p>hallo. ich habe eine Klasse erstellt mit 2 überladenden Konstruktoren:</p>
<pre><code class="language-cpp">class rectangle
{
private:
    int itsWidth;
    int itsLenght;
public:
    rectangle(); //HIER!
    rectangle(int widht,int lenght); //Hier!
    ~rectangle();
    int getWidth();
    int getLenght();

};
</code></pre>
<p>Konstrukoren wurden so initalisiert:</p>
<pre><code class="language-cpp">rectangle::rectangle()
{
    itsWidth=5;
    itsWidth=10;
}
rectangle::rectangle(int widht, int lenght)
{
    itsWidth=widht;
    itsLenght=lenght;

}
</code></pre>
<p>Wenn ich dem 2. Konstruktor nun Werte zuweisen möchte, klappt dies nicht:</p>
<pre><code class="language-cpp">rectangle rechteck1, rechteck2;
  rechteck2.rectangle(10,20);
</code></pre>
<p>main.cpp:25: error: invalid use of ‘class rectangle’</p>
<p>Hoffentlich kann mir einer helfen, danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1914102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914102</guid><dc:creator><![CDATA[cons2]]></dc:creator><pubDate>Thu, 17 Jun 2010 19:39:08 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 19:42:38 GMT]]></title><description><![CDATA[<p>rectangle rect2(10,20);</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1914104</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914104</guid><dc:creator><![CDATA[Mr X]]></dc:creator><pubDate>Thu, 17 Jun 2010 19:42:38 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 19:46:30 GMT]]></title><description><![CDATA[<p>no no no awful:</p>
<pre><code class="language-cpp">class rectangle
{
public:
    rectangle(int widht = 5,int length = 10);
};
</code></pre>
<pre><code class="language-cpp">rectangle::rectangle(int widht, int length)
   : width(width),
     length(length) 
{ }
</code></pre>
<p>leng<strong>th</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1914108</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914108</guid><dc:creator><![CDATA[designer don alejandro]]></dc:creator><pubDate>Thu, 17 Jun 2010 19:46:30 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 19:58:25 GMT]]></title><description><![CDATA[<p>Von unserem Tutor ist diese Form zugelassen:</p>
<pre><code class="language-cpp">//Konstruktoren überladen
                                                   Merke:
class rectangle
                                                   In C++ ist eine Methode nicht nur
{
                                                   durch ihren Namen, sondern auch
   private:
      int itsWidth;                                durch ihre Parameterliste bestimmt.
      int itsLength;
                                                   Methodenname und Parameterliste
   public:
                                                   werden zusammen als „Signatur“
      rectangle();
                                                   bezeichnet. So ist es also möglich
      rectangle(int width, int length);
      ~rectangle() { };                            zwei Methoden gleichen Namens zu
      int GetWidth() const { return itsWitdh; };   deklarieren, wenn sie sich in ihren
      int GetLength() const { return itsLength; };
                                                   Parametern unterscheiden – dabei
};
                                                   zählen sowohl Anzahl also auch
                                                   Datentypen und Reihenfolge. Dies
rectangle::rectangle()
{                                                  bezeichnet man als „überladen“.
   itsWidth = 5;
                                                   Das gleiche, wie im
   itsLength = 10;
                                                   nebenstehenden Beispiel mit dem
}
                                                   Konstruktor dargestellt, kann
rectangle::rectangle(int width, int length)        natürlich mit jeder x-beliebigen
{                                                  Klassenmethode durchgeführt
   itsWidth = width;
                                                   werden.
   itsLength = length;
}
</code></pre>
<p>Habe das Programm aber in 3 Elementen aufgeteil, in: main.cpp, rectangle.h(mit den Klassen), rectangle.cpp (mit Funktionsdefinitionen)</p>
<p>möchte aber den Konstruktor über die main.cpp ändern und die Form wie oben bei belassen.</p>
<p>Dieser Befehl klappt leider nicht:<br />
rechteck2.rectangle(10,20);</p>
<p>wieso nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1914118</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914118</guid><dc:creator><![CDATA[const2]]></dc:creator><pubDate>Thu, 17 Jun 2010 19:58:25 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 20:05:23 GMT]]></title><description><![CDATA[<p>natürlich nicht weil der standartkonstruktur bereits bei</p>
<pre><code class="language-cpp">rectangle rechteck1, rechteck2;
</code></pre>
<p>ausgeführt wurde.<br />
du musst wenn</p>
<pre><code class="language-cpp">rectangle rechteck1;
rectangle rechteck2(10,20);
</code></pre>
<p>machen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1914122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914122</guid><dc:creator><![CDATA[Jud4s]]></dc:creator><pubDate>Thu, 17 Jun 2010 20:05:23 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 20:07:08 GMT]]></title><description><![CDATA[<p>Mal abgesehen von deinem Rechtschreibfehler in GetWidth, wo die Variable wohl itsWidth heißen sollte, ist dein Code soweit in Ordnung.</p>
<p>Der Aufruf von rechteck2.rectangle(10,20); in der Form geht nicht. Der Konstruktor ist ja in dem Sinne keine Funktion, die man jederzeit aufrufen kann.</p>
<p>Bei der Instanziierung deiner Objekte aber dann in Ordnung:</p>
<pre><code class="language-cpp">rectangle a;
rectangle b(10,20);

cout &lt;&lt; a.GetWidth() &lt;&lt; &quot; &quot; &lt;&lt; a.GetLength() &lt;&lt; endl;
cout &lt;&lt; b.GetWidth() &lt;&lt; &quot; &quot; &lt;&lt; b.GetLength() &lt;&lt; endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1914123</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914123</guid><dc:creator><![CDATA[BasicMan01]]></dc:creator><pubDate>Thu, 17 Jun 2010 20:07:08 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 20:16:29 GMT]]></title><description><![CDATA[<p>danke, dass funktioniert!</p>
<pre><code class="language-cpp">leider, funktioniert das nur wenn ich die Werte eintrage, 
wenn ich sie aber als variablen übergeben will, wird 0 übergeben, wie kann ich das so lösen, dass ich die werte eingeben kann? 
int main()
{
    int breite=0;
    int lange=0;
    rectangle rechteck1;
    rectangle rechteck2(breite,lange);

      cout&lt;&lt;endl&lt;&lt;&quot;****** Rechteck2 ********&quot;;
    cout&lt;&lt;endl&lt;&lt;&quot;Breite eingeben: &quot;;
    cin&gt;&gt;breite;
    cout&lt;&lt;endl&lt;&lt;&quot;Länge eingeben: &quot;;
    cin&gt;&gt;lange;
    cout&lt;&lt;endl&lt;&lt;&quot;Breite: &quot;&lt;&lt;rechteck2.getWidth();
    cout&lt;&lt;endl&lt;&lt;&quot;Länge: &quot;&lt;&lt;rechteck2.getLength();

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1914126</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914126</guid><dc:creator><![CDATA[const2]]></dc:creator><pubDate>Thu, 17 Jun 2010 20:16:29 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor Problem on Thu, 17 Jun 2010 20:26:16 GMT]]></title><description><![CDATA[<p>indem du die Reihenfolge deiner Codezeilen änderst.</p>
<pre><code class="language-cpp">int main()
{
    int breite=0;
    int lange=0;

    // erst eingeben....
    cout&lt;&lt;endl&lt;&lt;&quot;****** Rechteck2 ********&quot;;
    cout&lt;&lt;endl&lt;&lt;&quot;Breite eingeben: &quot;;
    cin&gt;&gt;breite;
    cout&lt;&lt;endl&lt;&lt;&quot;Länge eingeben: &quot;;
    cin&gt;&gt;lange;

    // ... dann verwenden
    rectangle rechteck1;
    rectangle rechteck2(breite,lange);

    cout&lt;&lt;endl&lt;&lt;&quot;Breite: &quot;&lt;&lt;rechteck2.getWidth();
    cout&lt;&lt;endl&lt;&lt;&quot;Länge: &quot;&lt;&lt;rechteck2.getLength();

    return 0;
}
</code></pre>
<p>// Edit: YEAAAHHHH Mexiko-Frankreich 2-0</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1914136</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1914136</guid><dc:creator><![CDATA[BasicMan01]]></dc:creator><pubDate>Thu, 17 Jun 2010 20:26:16 GMT</pubDate></item></channel></rss>