<?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[Klassenfehler]]></title><description><![CDATA[<p>Hallo, ich habe ein Problem mit meiner Klasse.</p>
<p>In einer Funktion wird auf ein Shape drauf zugegriffen, was ich vorher in einer anderen Funktion erstellt habe.</p>
<p>Hier der Code:</p>
<h1>.h</h1>
<pre><code class="language-cpp">class  cls_Ampel{

   public:

     cls_Ampel();    // Konstruktor
     ~cls_Ampel();   // Destruktor

     initAmpel(int i_x, int i_y); // Ampel zeichnen

     changeStatus(TTimer *tim, int i_Rot, int i_Gelb, int i_Gruen);
     rot(TTimer *timer);       // Ereignis, was ausgeführt werden soll,
                  // wenn eine ampel auf rot springt

   private:

     TShape *sh_bgAmpel;
     TShape *ampel_red;
     TShape *ampel_yellow;
     TShape *ampel_green;
     int i_Status;   // Speicherung des Statuses
     int i_ampX;     // Position der X-Koordinate
     int i_ampY;     // Position der Y-Koordinate

};
</code></pre>
<h1>.cpp</h1>
<pre><code class="language-cpp">#include &quot;ampel_p1_u1.h&quot;
#include &quot;cls_ampel.h&quot;

// Konstruktor
cls_Ampel::cls_Ampel(){

   TShape *sh_bgAmpel;
    TShape *ampel_red;
     TShape *ampel_yellow;
     TShape *ampel_green;

   i_Status = 0;
}

// Ampel zeichnen
cls_Ampel::initAmpel(int x, int y){

  i_ampX = x;   // X-Koordinate speichern
  i_ampY = y;   // Y-Koordinate speichern

   //Hintergrund der Ampel
   TShape *sh_bgAmpel = new TShape(Form1);

   sh_bgAmpel-&gt;Shape = stRectangle;
   sh_bgAmpel-&gt;Width = 40;
   sh_bgAmpel-&gt;Height = 120;
   sh_bgAmpel-&gt;Top = i_ampY;
   sh_bgAmpel-&gt;Left = i_ampX;
   sh_bgAmpel-&gt;Brush-&gt;Color = clBlack;
   sh_bgAmpel-&gt;Parent = Form1;

   //Rot
   TShape *ampel_red = new TShape(Form1);

   ampel_red-&gt;Shape = stCircle;
   ampel_red-&gt;Brush-&gt;Color = clRed;
   ampel_red-&gt;Width = 35;
   ampel_red-&gt;Height = 35;
   ampel_red-&gt;Top = i_ampY+3;
   ampel_red-&gt;Left = i_ampX+3;
   ampel_red-&gt;Parent = Form1;

   //Gelb
   TShape *ampel_yellow = new TShape(Form1);

   ampel_yellow-&gt;Shape = stCircle;
   ampel_yellow-&gt;Brush-&gt;Color = clYellow;
   ampel_yellow-&gt;Width = 35;
   ampel_yellow-&gt;Height = 35;
   ampel_yellow-&gt;Top = ampel_red-&gt;Top + 38;
   ampel_yellow-&gt;Left = i_ampX + 3;
   ampel_yellow-&gt;Parent = Form1;

   //Gruen
   TShape *ampel_green = new TShape(Form1);

   ampel_green-&gt;Shape = stCircle;
   ampel_green-&gt;Brush-&gt;Color = clGreen;
   ampel_green-&gt;Width = 35;
   ampel_green-&gt;Height = 35;
   ampel_green-&gt;Top = ampel_yellow-&gt;Top + 38;
   ampel_green-&gt;Left = i_ampX + 3;
   ampel_green-&gt;Parent = Form1;

}

cls_Ampel::changeStatus(TTimer *tim, int i_Rot, int i_Gelb, int i_Gruen){

   switch(i_Status){

     case 0:
             ampel_red-&gt;Brush-&gt;Color = clYellow;
             i_Status = 1;
             tim-&gt;Interval = i_Rot;
             break;

     case 1: ampel_red-&gt;Brush-&gt;Color = clBlue;
             i_Status = 0;
             tim-&gt;Interval = i_Gelb;
             break;
   }
}

cls_Ampel::rot(TTimer *timer){

}

// Destruktor
cls_Ampel::~cls_Ampel(){

}
</code></pre>
<p>An den stellen in der switch case abfrage<br />
(ampel_red-&gt;Brush-&gt;Color = clYellow; oder ampel_red-&gt;Brush-&gt;Color = clBlue; )</p>
<p>, kommt eine &quot;Zugriffsverletzung an Adresse ...&quot; Fehlermeldung.</p>
<p>Wenn diese Zeile auskommentiert wird, kommt keine Meldung mehr.</p>
<p>Was habe ich in der Deklaration nun falsch gemacht. weiß da nicht weiter.</p>
<p>Ich bedanke mich schon einmal.</p>
<p>gruß<br />
thorsten</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/133874/klassenfehler</link><generator>RSS for Node</generator><lastBuildDate>Fri, 31 Jul 2026 21:38:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/133874.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 21 Jan 2006 19:18:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 19:23:30 GMT]]></title><description><![CDATA[<p>Hallo, ich habe ein Problem mit meiner Klasse.</p>
<p>In einer Funktion wird auf ein Shape drauf zugegriffen, was ich vorher in einer anderen Funktion erstellt habe.</p>
<p>Hier der Code:</p>
<h1>.h</h1>
<pre><code class="language-cpp">class  cls_Ampel{

   public:

     cls_Ampel();    // Konstruktor
     ~cls_Ampel();   // Destruktor

     initAmpel(int i_x, int i_y); // Ampel zeichnen

     changeStatus(TTimer *tim, int i_Rot, int i_Gelb, int i_Gruen);
     rot(TTimer *timer);       // Ereignis, was ausgeführt werden soll,
                  // wenn eine ampel auf rot springt

   private:

     TShape *sh_bgAmpel;
     TShape *ampel_red;
     TShape *ampel_yellow;
     TShape *ampel_green;
     int i_Status;   // Speicherung des Statuses
     int i_ampX;     // Position der X-Koordinate
     int i_ampY;     // Position der Y-Koordinate

};
</code></pre>
<h1>.cpp</h1>
<pre><code class="language-cpp">#include &quot;ampel_p1_u1.h&quot;
#include &quot;cls_ampel.h&quot;

// Konstruktor
cls_Ampel::cls_Ampel(){

   TShape *sh_bgAmpel;
    TShape *ampel_red;
     TShape *ampel_yellow;
     TShape *ampel_green;

   i_Status = 0;
}

// Ampel zeichnen
cls_Ampel::initAmpel(int x, int y){

  i_ampX = x;   // X-Koordinate speichern
  i_ampY = y;   // Y-Koordinate speichern

   //Hintergrund der Ampel
   TShape *sh_bgAmpel = new TShape(Form1);

   sh_bgAmpel-&gt;Shape = stRectangle;
   sh_bgAmpel-&gt;Width = 40;
   sh_bgAmpel-&gt;Height = 120;
   sh_bgAmpel-&gt;Top = i_ampY;
   sh_bgAmpel-&gt;Left = i_ampX;
   sh_bgAmpel-&gt;Brush-&gt;Color = clBlack;
   sh_bgAmpel-&gt;Parent = Form1;

   //Rot
   TShape *ampel_red = new TShape(Form1);

   ampel_red-&gt;Shape = stCircle;
   ampel_red-&gt;Brush-&gt;Color = clRed;
   ampel_red-&gt;Width = 35;
   ampel_red-&gt;Height = 35;
   ampel_red-&gt;Top = i_ampY+3;
   ampel_red-&gt;Left = i_ampX+3;
   ampel_red-&gt;Parent = Form1;

   //Gelb
   TShape *ampel_yellow = new TShape(Form1);

   ampel_yellow-&gt;Shape = stCircle;
   ampel_yellow-&gt;Brush-&gt;Color = clYellow;
   ampel_yellow-&gt;Width = 35;
   ampel_yellow-&gt;Height = 35;
   ampel_yellow-&gt;Top = ampel_red-&gt;Top + 38;
   ampel_yellow-&gt;Left = i_ampX + 3;
   ampel_yellow-&gt;Parent = Form1;

   //Gruen
   TShape *ampel_green = new TShape(Form1);

   ampel_green-&gt;Shape = stCircle;
   ampel_green-&gt;Brush-&gt;Color = clGreen;
   ampel_green-&gt;Width = 35;
   ampel_green-&gt;Height = 35;
   ampel_green-&gt;Top = ampel_yellow-&gt;Top + 38;
   ampel_green-&gt;Left = i_ampX + 3;
   ampel_green-&gt;Parent = Form1;

}

cls_Ampel::changeStatus(TTimer *tim, int i_Rot, int i_Gelb, int i_Gruen){

   switch(i_Status){

     case 0:
             ampel_red-&gt;Brush-&gt;Color = clYellow;
             i_Status = 1;
             tim-&gt;Interval = i_Rot;
             break;

     case 1: ampel_red-&gt;Brush-&gt;Color = clBlue;
             i_Status = 0;
             tim-&gt;Interval = i_Gelb;
             break;
   }
}

cls_Ampel::rot(TTimer *timer){

}

// Destruktor
cls_Ampel::~cls_Ampel(){

}
</code></pre>
<p>An den stellen in der switch case abfrage<br />
(ampel_red-&gt;Brush-&gt;Color = clYellow; oder ampel_red-&gt;Brush-&gt;Color = clBlue; )</p>
<p>, kommt eine &quot;Zugriffsverletzung an Adresse ...&quot; Fehlermeldung.</p>
<p>Wenn diese Zeile auskommentiert wird, kommt keine Meldung mehr.</p>
<p>Was habe ich in der Deklaration nun falsch gemacht. weiß da nicht weiter.</p>
<p>Ich bedanke mich schon einmal.</p>
<p>gruß<br />
thorsten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/972390</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972390</guid><dc:creator><![CDATA[ThorstenSteinberg]]></dc:creator><pubDate>Sat, 21 Jan 2006 19:23:30 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 19:43:44 GMT]]></title><description><![CDATA[<p>bist du dir auch 100% sicher dass du auch diese initAmpel() funktion aufgerufen hast und somit die klassen mit new erstellt hast?</p>
<p>mfg<br />
BigNeal</p>
]]></description><link>https://www.c-plusplus.net/forum/post/972415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972415</guid><dc:creator><![CDATA[BigNeal]]></dc:creator><pubDate>Sat, 21 Jan 2006 19:43:44 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 19:46:22 GMT]]></title><description><![CDATA[<p>Du darfst in der Init- Routine keine neue Variablen anlegen!!!</p>
<p>Nimm einfach die in private: angekegten!</p>
<p>TShape *ampel_red = new TShape(Form1);<br />
wird<br />
ampel_red = new TShape(Form1);</p>
]]></description><link>https://www.c-plusplus.net/forum/post/972416</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972416</guid><dc:creator><![CDATA[DerAltenburger]]></dc:creator><pubDate>Sat, 21 Jan 2006 19:46:22 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 19:47:45 GMT]]></title><description><![CDATA[<p>DerAltenburger schrieb:</p>
<blockquote>
<p>Du darfst in der Init- Routine keine neue Variablen anlegen!!!</p>
</blockquote>
<p>stimmt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
so genau habe ich mir das gar nicht angeschaut <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
aber habe mich gewundert was das im konstruktor sollte</p>
<p>mfg<br />
BigNeal</p>
]]></description><link>https://www.c-plusplus.net/forum/post/972417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972417</guid><dc:creator><![CDATA[BigNeal]]></dc:creator><pubDate>Sat, 21 Jan 2006 19:47:45 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 19:49:01 GMT]]></title><description><![CDATA[<p>ok, Du hast da etwas gründlich mißverstanden.<br />
Du deklarierst zwar formularweit gültige Zeiger auf die TShapes, aber Du verwendest diese nicht. Nur dort, wo Du TShape* blablabla = new TShape(); verwendest erzeugst Du tatsächlich eine verwendbare Instanz. Da Du jedoch den Typ bei der Erzeugung nochmals angibst, verwendest Du nicht die formalurweit gültigen Zeiger aus dem Header der Form, sondern erzeugst neue Zeiger, gleichen Namens, die aber nur innerhalb der Funktion gültig sind. Am Ende der Funktion sind diese nicht mehr verwendbar, da der Zeiger seinen Gültigkeitsbereich verläßt und Du hast keine Möglichkeit mehr auf das TShape-Objekt zuzugreifen.<br />
Wenn Du (sinnvollerweise im Konstruktor) die TShapes erzeugst und im Destruktor wieder 'deletest' kannst Du in allen Funktionen mit ihnen arbeiten.<br />
Im Konstruktor:</p>
<pre><code class="language-cpp">ampel_yellow = new TShape(Form1);
</code></pre>
<p>Grüße Joe_M.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/972418</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972418</guid><dc:creator><![CDATA[zufaulzumeinloggen]]></dc:creator><pubDate>Sat, 21 Jan 2006 19:49:01 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 19:49:55 GMT]]></title><description><![CDATA[<p>Das soll mir eine Lehre sein, so lange Beiträge zu tippen... <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/972420</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972420</guid><dc:creator><![CDATA[zufaulzumeinloggen]]></dc:creator><pubDate>Sat, 21 Jan 2006 19:49:55 GMT</pubDate></item><item><title><![CDATA[Reply to Klassenfehler on Sat, 21 Jan 2006 20:51:09 GMT]]></title><description><![CDATA[<p>Mensch jungs, ich danke euch!!</p>
<p>es sind halt manchmal die kleinen dinge die aufhalten.</p>
<p>gruß thorsten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/972436</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/972436</guid><dc:creator><![CDATA[ThorstenSteinberg]]></dc:creator><pubDate>Sat, 21 Jan 2006 20:51:09 GMT</pubDate></item></channel></rss>