<?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] static Variable macht Probleme]]></title><description><![CDATA[<p>Hi, ich habe für ein Spiel folgende Klasse:</p>
<pre><code class="language-cpp">#ifndef SHOT
#define SHOT
#include &quot;ShotKind.hpp&quot;
#include &quot;Time.hpp&quot;
#include &quot;EventList.hpp&quot;

class ShotKind;
class EventList;

class Shot
{
    private:
        ShotKind* Kind;
        Position Start;
        Position End;
        bool Detonated;
        Shot* Next;
        double Starttime;
        static Time* Clock;
        static EventList* EventList;

    public:
        Shot(Position start, Position end)
        : Start(start), End(end), Detonated(false), Next(0)
        {
            Starttime = Clock-&gt;GetTime();
        }
        bool Draw();
        bool GetDetonated() {return Detonated;}
};

#endif
</code></pre>
<p>Die beiden static Variablen werden Global befüllt:</p>
<pre><code class="language-cpp">//etc.
#include &quot;Time.hpp&quot;
#include &quot;EventList.hpp&quot;

    //Static-Deklarationen
    Time Clock();
    EventList EventList();

    Time* Shot::Clock = &amp;Clock;
    Time* Event::Clock = &amp;Clock;
    Time* EventList::Clock = &amp;Clock;
    EventList* Shot::EventList = &amp;EventList;

int main()
{
//etc.
</code></pre>
<p>Gegen den Zeiger auf Time hat mein Compiler nichts, aber gegen den auf EventList:</p>
<blockquote>
<p>C:\...\Shot.hpp|20|error: declaration of <code>EventList*Shot::EventList'| C:\\...\\EventList.hpp|8|error: changes meaning of \</code>EventList' from `class EventList'|<br />
||=== Build finished: 2 errors, 0 warnings ===|</p>
</blockquote>
<p>Hier nochmal die Problemklasse:</p>
<pre><code class="language-cpp">#ifndef EVENTLIST
#define EVENTLIST
#include &quot;Position.hpp&quot;
#include &quot;Event.hpp&quot;
#include &quot;Time.hpp&quot;

class EventList
{
    private:
        Event* FirstEvent;
        static Time* Clock;

    public:
        EventList()
        : FirstEvent(0) {}
        EventList(Event* firstEvent)
        : FirstEvent(firstEvent) {}
        void AddEvent(int Kind, Position Pos, double Radius, int Strange);
        void AddEvent(int Kind, Position Pos, double Radius, int Strange,
                      double Duration, double DamageDuration);
        Event* CheckEvent(Position Pos, double Radius);
        Event* CheckEvent(Event* AfterEvent, Position Pos, double Radius);
        Event* CheckEvent(Position Pos, double Width, double Height);
        Event* CheckEvent(Event* AfterEvent, Position Pos, double Width,
                          double Height);
        int CheckStrange(Event* Event, Position Pos, double Radius);
        int CheckStrange(Event* Event, Position Pos, double Width, double Height);
        void CleanUp();
};

#endif
</code></pre>
<p>Ich komme einfach nicht drauf. Was mache ich anders als bei Time, dass hier die static-Variable nicht funktioniert?</p>
<p>Danke für die Hilfe im voraus. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/259937/gelöst-static-variable-macht-probleme</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 16:26:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/259937.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 29 Jan 2010 15:29:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:59:06 GMT]]></title><description><![CDATA[<p>Hi, ich habe für ein Spiel folgende Klasse:</p>
<pre><code class="language-cpp">#ifndef SHOT
#define SHOT
#include &quot;ShotKind.hpp&quot;
#include &quot;Time.hpp&quot;
#include &quot;EventList.hpp&quot;

class ShotKind;
class EventList;

class Shot
{
    private:
        ShotKind* Kind;
        Position Start;
        Position End;
        bool Detonated;
        Shot* Next;
        double Starttime;
        static Time* Clock;
        static EventList* EventList;

    public:
        Shot(Position start, Position end)
        : Start(start), End(end), Detonated(false), Next(0)
        {
            Starttime = Clock-&gt;GetTime();
        }
        bool Draw();
        bool GetDetonated() {return Detonated;}
};

#endif
</code></pre>
<p>Die beiden static Variablen werden Global befüllt:</p>
<pre><code class="language-cpp">//etc.
#include &quot;Time.hpp&quot;
#include &quot;EventList.hpp&quot;

    //Static-Deklarationen
    Time Clock();
    EventList EventList();

    Time* Shot::Clock = &amp;Clock;
    Time* Event::Clock = &amp;Clock;
    Time* EventList::Clock = &amp;Clock;
    EventList* Shot::EventList = &amp;EventList;

int main()
{
//etc.
</code></pre>
<p>Gegen den Zeiger auf Time hat mein Compiler nichts, aber gegen den auf EventList:</p>
<blockquote>
<p>C:\...\Shot.hpp|20|error: declaration of <code>EventList*Shot::EventList'| C:\\...\\EventList.hpp|8|error: changes meaning of \</code>EventList' from `class EventList'|<br />
||=== Build finished: 2 errors, 0 warnings ===|</p>
</blockquote>
<p>Hier nochmal die Problemklasse:</p>
<pre><code class="language-cpp">#ifndef EVENTLIST
#define EVENTLIST
#include &quot;Position.hpp&quot;
#include &quot;Event.hpp&quot;
#include &quot;Time.hpp&quot;

class EventList
{
    private:
        Event* FirstEvent;
        static Time* Clock;

    public:
        EventList()
        : FirstEvent(0) {}
        EventList(Event* firstEvent)
        : FirstEvent(firstEvent) {}
        void AddEvent(int Kind, Position Pos, double Radius, int Strange);
        void AddEvent(int Kind, Position Pos, double Radius, int Strange,
                      double Duration, double DamageDuration);
        Event* CheckEvent(Position Pos, double Radius);
        Event* CheckEvent(Event* AfterEvent, Position Pos, double Radius);
        Event* CheckEvent(Position Pos, double Width, double Height);
        Event* CheckEvent(Event* AfterEvent, Position Pos, double Width,
                          double Height);
        int CheckStrange(Event* Event, Position Pos, double Radius);
        int CheckStrange(Event* Event, Position Pos, double Width, double Height);
        void CleanUp();
};

#endif
</code></pre>
<p>Ich komme einfach nicht drauf. Was mache ich anders als bei Time, dass hier die static-Variable nicht funktioniert?</p>
<p>Danke für die Hilfe im voraus. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847095</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:59:06 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:37:00 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">//Static-Deklarationen
    Time Clock();             // !!! Funktionsdeklaration!!!
    EventList EventList();    // Name ändern

    Time* Shot::Clock = &amp;Clock;
    Time* Event::Clock = &amp;Clock;
    Time* EventList::Clock = &amp;Clock;
    EventList* Shot::EventList = &amp;EventList;
</code></pre>
<p>Ansonsten gehört die Initialisierung von static-Membern in die Implementierungsdatei der Klasse. wenn du das später wieder verwenden willst musst du jedesmal in der main.cpp wieder alles zusammenfrickeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847098</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847098</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:37:00 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:39:08 GMT]]></title><description><![CDATA[<p>Ich habe mir gerade gedacht, dass es dem Compiler vielleicht nicht passt, dass der zeiger auch EventList heißt, hab in deshalb in EventList* Events geändert. Jetzt tritt der Fehler nicht mehr auf, aber dafür ein anderes:</p>
<pre><code class="language-cpp">#include &quot;Shot.hpp&quot;
#include &quot;Exceptions.hpp&quot;
#include &quot;Time.hpp&quot;
#include &quot;EventList.hpp&quot;
#include &quot;Event.hpp&quot;

    //Static-Deklarationen
    Time Clock;
    EventList EventList;

    Time* Shot::Clock = &amp;Clock;
    Time* Event::Clock = &amp;Clock;
    Time* EventList::Clock = &amp;Clock;
    EventList* Shot::Events = &amp;EventList;

int main()
{
//etc.
</code></pre>
<blockquote>
<p>C:\...\main.cpp|18|error: cannot convert `Time**' to `Time*' in initialization|<br />
C:\...\main.cpp|19|error: cannot convert `Time**' to `Time*' in initialization|<br />
C:\...\main.cpp|20|error: cannot convert `Time**' to `Time*' in initialization|<br />
C:\...\main.cpp|21|error: expected constructor, destructor, or type conversion before '*' token|<br />
||=== Build finished: 4 errors, 0 warnings ===|</p>
</blockquote>
<p>Ich hab nicht den leisesten Hauch einer Ahnung, wieso er meint ich hätte Time** angelegt. Und scheinbar erkennt er die Klasse EventList nicht. Vielleicht ist das auch noch der selbe Fehler, der schon vorher vorhanden war, nur, dass er sich anders ausdrückt. Bin grad echt ratlos. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847099</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:39:08 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:39:00 GMT]]></title><description><![CDATA[<p>Mach mal die Klammern hinter den statischen Objekten weg, der Compiler interpretiert sie als Funktionen und nicht als Objekte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847102</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:39:00 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:40:10 GMT]]></title><description><![CDATA[<p>Ja, habs grad editiert. is mir auch eben aufgefallen, ändert aber das Problem nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847103</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847103</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:40:10 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:42:01 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/20104">@l</a>'abra d'or, ich kann die also auch in der Header-Datei außerhalb der Klassendefinition initialisieren? Wusste ich gar nicht. ich dachte immer die muss man global oder zumindest am Anfang der main{} initialisieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847104</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847104</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:42:01 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:45:20 GMT]]></title><description><![CDATA[<p>Little Programmer schrieb:</p>
<blockquote>
<pre><code class="language-cpp">EventList EventList;
</code></pre>
</blockquote>
<p>Deklariert ein Objekt Eventlist des Typs Eventlist. Jede folgende Verwendung des Namens bezieht sich auf das Objekt nicht den Typ, der Klassenname wird durch die Objektdeklaration verdeckt.<br />
Möglich ist die Verwendung per Klassenschlüsselwort, also</p>
<pre><code class="language-cpp">class EventList* Shot::Events = &amp;EventList;
</code></pre>
<p>besser ist es aber, die doppelte Verwendung des Bezeichners zu vermeiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847106</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:45:20 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:49:32 GMT]]></title><description><![CDATA[<p>Danke, so ist die aktuelle Version:</p>
<pre><code class="language-cpp">#include &lt;SFML/Graphics.hpp&gt;
#include &quot;Shot.hpp&quot;
#include &quot;Exceptions.hpp&quot;
#include &quot;Time.hpp&quot;
#include &quot;EventList.hpp&quot;
#include &quot;Event.hpp&quot;

using namespace std;

    //Globale Variablen
    sf::RenderWindow Screen(sf::VideoMode(1024, 768, 32), &quot;Programm&quot;, sf::Style::Fullscreen);

    //Static-Deklarationen
    Time Clock(&amp;Screen);
    EventList Events;

    Time* Shot::Clock = &amp;Clock;
    Time* Event::Clock = &amp;Clock;
    Time* EventList::Clock = &amp;Clock;
    EventList* Shot::Events = &amp;Events;

int main()
{
//etc.
</code></pre>
<p>Fehlermeldung unverändert. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<blockquote>
<p>C:\...\main.cpp|22|error: cannot convert `Time**' to `Time*' in initialization|<br />
C:\...\main.cpp|23|error: cannot convert `Time**' to `Time*' in initialization|<br />
C:\...\main.cpp|24|error: cannot convert `Time**' to `Time*' in initialization|<br />
C:\...\main.cpp|25|error: cannot convert `EventList**' to `EventList*' in initialization|<br />
||=== Build finished: 4 errors, 0 warnings ===|</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1847111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847111</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:49:32 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:59:16 GMT]]></title><description><![CDATA[<p>Der Initialisierer einer statischen Membervariablen wird im Scope der Klasse auswertet (Ebenso wie bereits die Parametertypen in einer Memberfunktionsdefinion im Scope der Klasse ausgewertet werden), folglich verweist &amp;Clock in</p>
<pre><code class="language-cpp">Time* Shot::Clock = &amp;Clock
</code></pre>
<p>auf Shot::Clock, nicht auf die zuvor definierte globale Clock-Variable. Folglich:</p>
<pre><code class="language-cpp">Time* Shot::Clock = &amp;::Clock
</code></pre>
<p>Der Rest analog.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847115</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847115</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:59:16 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:58:40 GMT]]></title><description><![CDATA[<p>Gelöst. War nur ein Namenskonflikt:</p>
<pre><code class="language-cpp">//Static-Deklarationen
    Time GeneralClock(&amp;Screen);
    EventList AllEvents;

    Time* Shot::Clock = &amp;GeneralClock;
    Time* Event::Clock = &amp;GeneralClock;
    Time* EventList::Clock = &amp;GeneralClock;
    EventList* Shot::Events = &amp;AllEvents;
</code></pre>
<p>Die Zeiger hießen gleich wie das Objekt auf das sie zeigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847116</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847116</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:58:40 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 15:59:42 GMT]]></title><description><![CDATA[<p>Danke camper. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847117</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847117</guid><dc:creator><![CDATA[Little Programmer]]></dc:creator><pubDate>Fri, 29 Jan 2010 15:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] static Variable macht Probleme on Fri, 29 Jan 2010 16:12:01 GMT]]></title><link>https://www.c-plusplus.net/forum/post/1847119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847119</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 29 Jan 2010 16:12:01 GMT</pubDate></item></channel></rss>