<?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[Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt]]></title><description><![CDATA[<p>Hallo erstmal,</p>
<p>schon wieder habe ich ein Problem mit meinem Programm: Ein erzeugter Mensch wird außerhalb von main() nicht mehr erkannt. Mein Programm:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &quot;mensch.h&quot;

using namespace std;

int main()
{

Mensch nexus;
Mensch gorn;

nexus.erzeuge(&quot;Nexus&quot;,250,50);
gorn.erzeuge(&quot;Gorn&quot;,250,50);

    while (true) {
/********************Abfragebereich von Aktion und target**************************/
/*  int b;
    while (b != 1) {  */

cout&lt;&lt;&quot;Bitte gebe an, welche Aktion du diese Runde ausführen möchtest (Fuer eine komplette Liste, bitte -a schreiben):\n&quot;;
string action;
getline ( cin , action );

if (action == &quot;Trank&quot;)
      {
           cout&lt;&lt;&quot;\n\nBitte gebe an, welchen Trank du trinken willst (Für eine volle Liste aller deiner Tränke bitte -p schreiben):\n&quot;;
           string pot;
           int sore;
           getline ( cin , pot );

           sore = nexus.drinkpotion(pot);
           if (sore == 1) {
                    cout&lt;&lt;&quot;Du besitzt diesen Trank nicht!&quot;;
           } else {
           if (pot == &quot;Lebenspunkte 1&quot; || pot == &quot;Lebenspunkte 2&quot; || pot == &quot;Lebenspunkte 3&quot;) {
                   cout&lt;&lt;&quot;Nexus heilt sich um &quot;&lt;&lt;sore&lt;&lt;&quot; Lebenspunkte!&quot;;
           } else if (pot == &quot;Mana 1&quot; || pot == &quot;Mana 2&quot; || pot == &quot;Mana 3&quot;) {
                   cout&lt;&lt;&quot;Nexus erhöht sein Mana um &quot;&lt;&lt;sore&lt;&lt;&quot; Punkte!\n\n\n\n&quot;;
           }
           }
      } else if (action == &quot;Zauber&quot;) {
             cout&lt;&lt;&quot;Bitte gebe an, welchen Zauber du wirken willst (Fuer eine volle Liste aller erlernten Zauber, bitte -s schreiben):\n&quot;;
             string spl;
             getline(cin,spl);
             int rest;
             rest = checkspell(spl);
             if (rest != 1) {
                      cout&lt;&lt;&quot;Zauber existiert nicht!\n\n&quot;;
                      continue;
             }
             int c;
             while (c != 1) {
             cout&lt;&lt;&quot;Bitte gebe an, auf wen du den Zauber wirken willst (Fuer eine volle Liste von Gegnern und ihren Werten, bitte -v schreiben)\n&quot;;
             string tgt;
             getline(cin,tgt);
             int resth;
             nexus.castspell(&quot;Feuerball&quot;,&quot;Gorn&quot;);
/*             resth = checkenemy(tgt);
             if (resth != 1) {
                       cout&lt;&lt;&quot;Ziel existiert nicht!\n\n&quot;;
                                    continue;
             }*/
             c = 1;
             }

      }

}
}

int Mensch::castspell (const string spell , const string target) {
    int dmg;
    int mnm;
    if (spell == &quot;Feuerball&quot;) {
             dmg = 20;
             mnm = 10;
    } else if (spell == &quot;Feuerpfeil&quot;) {
              dmg = 10;
              mnm = 5;
    } else if (spell == &quot;Eispfeil&quot;) {
              dmg = 15;
              mnm = 7;
    } else {
              return 0;
    }
    cout&lt;&lt;name&lt;&lt;&quot; wirkt den Zauber &quot;&lt;&lt;spell&lt;&lt;&quot; auf &quot;&lt;&lt;target&lt;&lt;&quot;!\n\n&quot;;
    mana -= mnm;
    if (target == &quot;Gorn&quot;) {
               gorn.life -= dmg;

    }
}
</code></pre>
<p>es ist noch nicht fertig, also nicht wundern wenns noch größere Lücken enthält!<br />
Der Compiler gibt mir in Zeile 95 den Fehler aus, gorn sei undeklariert. gorn wurde aber im main Bereich deklariert. Ich nehme an, in der Funktion ist das nicht mehr sichtbar, denn im main Bereich funktioniert das ganze wunderbar. Meine Frage:<br />
Wie kriege ich es hin, dass gorn auch in dieser Funktion mit korrekten Werten noch deklariert ist? Geht das auch in einer anderen Datei (mensch.h, dort wo ich auch die anderen Funktionen habe)?</p>
<p>P.S.: Die angebliche 'Funktion' gorn.life -= dmg; ist garkeine Funktion. In der Klasse von gorn ist ein Wert 'life' definiert. Dieser soll hier heruntergesetzt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/186507/objekt-einer-klasse-wird-außerhalb-von-main-nicht-mehr-erkannt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 10:06:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/186507.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Jul 2007 16:40:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 16:40:27 GMT]]></title><description><![CDATA[<p>Hallo erstmal,</p>
<p>schon wieder habe ich ein Problem mit meinem Programm: Ein erzeugter Mensch wird außerhalb von main() nicht mehr erkannt. Mein Programm:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &quot;mensch.h&quot;

using namespace std;

int main()
{

Mensch nexus;
Mensch gorn;

nexus.erzeuge(&quot;Nexus&quot;,250,50);
gorn.erzeuge(&quot;Gorn&quot;,250,50);

    while (true) {
/********************Abfragebereich von Aktion und target**************************/
/*  int b;
    while (b != 1) {  */

cout&lt;&lt;&quot;Bitte gebe an, welche Aktion du diese Runde ausführen möchtest (Fuer eine komplette Liste, bitte -a schreiben):\n&quot;;
string action;
getline ( cin , action );

if (action == &quot;Trank&quot;)
      {
           cout&lt;&lt;&quot;\n\nBitte gebe an, welchen Trank du trinken willst (Für eine volle Liste aller deiner Tränke bitte -p schreiben):\n&quot;;
           string pot;
           int sore;
           getline ( cin , pot );

           sore = nexus.drinkpotion(pot);
           if (sore == 1) {
                    cout&lt;&lt;&quot;Du besitzt diesen Trank nicht!&quot;;
           } else {
           if (pot == &quot;Lebenspunkte 1&quot; || pot == &quot;Lebenspunkte 2&quot; || pot == &quot;Lebenspunkte 3&quot;) {
                   cout&lt;&lt;&quot;Nexus heilt sich um &quot;&lt;&lt;sore&lt;&lt;&quot; Lebenspunkte!&quot;;
           } else if (pot == &quot;Mana 1&quot; || pot == &quot;Mana 2&quot; || pot == &quot;Mana 3&quot;) {
                   cout&lt;&lt;&quot;Nexus erhöht sein Mana um &quot;&lt;&lt;sore&lt;&lt;&quot; Punkte!\n\n\n\n&quot;;
           }
           }
      } else if (action == &quot;Zauber&quot;) {
             cout&lt;&lt;&quot;Bitte gebe an, welchen Zauber du wirken willst (Fuer eine volle Liste aller erlernten Zauber, bitte -s schreiben):\n&quot;;
             string spl;
             getline(cin,spl);
             int rest;
             rest = checkspell(spl);
             if (rest != 1) {
                      cout&lt;&lt;&quot;Zauber existiert nicht!\n\n&quot;;
                      continue;
             }
             int c;
             while (c != 1) {
             cout&lt;&lt;&quot;Bitte gebe an, auf wen du den Zauber wirken willst (Fuer eine volle Liste von Gegnern und ihren Werten, bitte -v schreiben)\n&quot;;
             string tgt;
             getline(cin,tgt);
             int resth;
             nexus.castspell(&quot;Feuerball&quot;,&quot;Gorn&quot;);
/*             resth = checkenemy(tgt);
             if (resth != 1) {
                       cout&lt;&lt;&quot;Ziel existiert nicht!\n\n&quot;;
                                    continue;
             }*/
             c = 1;
             }

      }

}
}

int Mensch::castspell (const string spell , const string target) {
    int dmg;
    int mnm;
    if (spell == &quot;Feuerball&quot;) {
             dmg = 20;
             mnm = 10;
    } else if (spell == &quot;Feuerpfeil&quot;) {
              dmg = 10;
              mnm = 5;
    } else if (spell == &quot;Eispfeil&quot;) {
              dmg = 15;
              mnm = 7;
    } else {
              return 0;
    }
    cout&lt;&lt;name&lt;&lt;&quot; wirkt den Zauber &quot;&lt;&lt;spell&lt;&lt;&quot; auf &quot;&lt;&lt;target&lt;&lt;&quot;!\n\n&quot;;
    mana -= mnm;
    if (target == &quot;Gorn&quot;) {
               gorn.life -= dmg;

    }
}
</code></pre>
<p>es ist noch nicht fertig, also nicht wundern wenns noch größere Lücken enthält!<br />
Der Compiler gibt mir in Zeile 95 den Fehler aus, gorn sei undeklariert. gorn wurde aber im main Bereich deklariert. Ich nehme an, in der Funktion ist das nicht mehr sichtbar, denn im main Bereich funktioniert das ganze wunderbar. Meine Frage:<br />
Wie kriege ich es hin, dass gorn auch in dieser Funktion mit korrekten Werten noch deklariert ist? Geht das auch in einer anderen Datei (mensch.h, dort wo ich auch die anderen Funktionen habe)?</p>
<p>P.S.: Die angebliche 'Funktion' gorn.life -= dmg; ist garkeine Funktion. In der Klasse von gorn ist ein Wert 'life' definiert. Dieser soll hier heruntergesetzt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321707</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321707</guid><dc:creator><![CDATA[Alemarius Nexus]]></dc:creator><pubDate>Mon, 09 Jul 2007 16:40:27 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 16:47:49 GMT]]></title><description><![CDATA[<p>Wenn du etwas in main erstellst, warum sollte es dann ausserhalb von main bekannt sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321713</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Mon, 09 Jul 2007 16:47:49 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 16:49:33 GMT]]></title><description><![CDATA[<p>Wie wäre es, wenn du das Ziel nicht als String, sondern gleich als Mensch&amp; übergibst? Dann könntest du dir auch dieses if-else-Geraffel schenken und sofort target.life -= dmg; schreiben.</p>
<p>Außerdem wäre eine Spell-Klasse toll. Würde nämlich auch den Code stark vereinfachen.</p>
<p>Und lass den Quatsch mit &quot;erzeuge&quot;. Dafür gibt's Konstruktoren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321715</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321715</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Mon, 09 Jul 2007 16:49:33 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 17:46:25 GMT]]></title><description><![CDATA[<blockquote>
<p>Wenn du etwas in main erstellst, warum sollte es dann ausserhalb von main bekannt sein?</p>
</blockquote>
<p>Ich wollte ja auch wissen, wie man es trotzdem außerhalb von main aufrufen (z.B. an Funktion übergeben) kann <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<blockquote>
<p>Wie wäre es, wenn du das Ziel nicht als String, sondern gleich als Mensch&amp; übergibst?</p>
</blockquote>
<p>Könntest du das mal als Beispiel (auch mit Klassen, wenn möglich) machen? Ich weiß nicht, wie man das richtig an eine Funktion übergibt und ob man gorn oder Mensch oder was auch immer als Referenz benutzt. Und wie muss die Deklaration der Funktion dann aussehen?</p>
<p>Danke für die Hilfe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321752</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321752</guid><dc:creator><![CDATA[Alemarius Nexus]]></dc:creator><pubDate>Mon, 09 Jul 2007 17:46:25 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 18:21:13 GMT]]></title><description><![CDATA[<p>Alemarius Nexus schrieb:</p>
<blockquote>
<blockquote>
<p>Wenn du etwas in main erstellst, warum sollte es dann ausserhalb von main bekannt sein?</p>
</blockquote>
<p>Ich wollte ja auch wissen, wie man es trotzdem außerhalb von main aufrufen (z.B. an Funktion übergeben) kann <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
</blockquote>
<p>na bitte. da hast du doch schon eine loesung <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>
<p>die andere waere, das objekt nicht in main zu erstellen sondern ein level darueber: als vor main.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321772</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Mon, 09 Jul 2007 18:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 18:26:20 GMT]]></title><description><![CDATA[<p>Kurz OT:<br />
Darf ich fragen was das wird? Sieht aus wie ein Textbasiertes Rollenspiel finde ich <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/1321774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321774</guid><dc:creator><![CDATA[Thundereye]]></dc:creator><pubDate>Mon, 09 Jul 2007 18:26:20 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 19:13:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &quot;mensch.h&quot;

using namespace std;

Mensch nexus;
Mensch gorn;

nexus.erzeuge(&quot;Nexus&quot;,250,50);
gorn.erzeuge(&quot;Gorn&quot;,250,50);

int main()
{

    while (true) {
/********************Abfragebereich von Aktion und target**************************/
/*  int b;
    while (b != 1) {  */

cout&lt;&lt;&quot;Bitte gebe an, welche Aktion du diese Runde ausführen möchtest (Fuer eine komplette Liste, bitte -a schreiben):\n&quot;;
string action;
getline ( cin , action );

if (action == &quot;Trank&quot;)
...
...
...
</code></pre>
<p>Wenn ich das so kompiliere kommt der Fehler: expected constructor, destructor or type conversion before '.' token in den Zeilen 11 und 12 (auch ein paar andere Fehler, das sind aber wahrscheinlich Folgefeher daraus).</p>
<blockquote>
<p>Darf ich fragen was das wird? Sieht aus wie ein Textbasiertes Rollenspiel finde ich</p>
</blockquote>
<p>Ja sowas ähnliches, wird aber nur zum lernen, was daraus wird weiß ich selber noch nicht (bin noch Anfänger).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321796</guid><dc:creator><![CDATA[Alemarius Nexus]]></dc:creator><pubDate>Mon, 09 Jul 2007 19:13:36 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 19:30:28 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<pre><code>nexus.erzeuge(&quot;Nexus&quot;,250,50);
gorn.erzeuge(&quot;Gorn&quot;,250,50);
</code></pre>
<p>wieder schön in die main-Funktion! <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>
<pre><code>int Mensch::castspell (const string spell , Mensch *target) {
    int dmg;
    int mnm;
    if (spell == &quot;Feuerball&quot;) {
             dmg = 20;
             mnm = 10;
    } else if (spell == &quot;Feuerpfeil&quot;) {
              dmg = 10;
              mnm = 5;
    } else if (spell == &quot;Eispfeil&quot;) {
              dmg = 15;
              mnm = 7;
    } else {
              return 0;
    }
    cout&lt;&lt;name&lt;&lt;&quot; wirkt den Zauber &quot;&lt;&lt;spell&lt;&lt;&quot; auf &quot;&lt;&lt; target.getName() &lt;&lt;&quot;!\n\n&quot;;
    mana -= mnm;

    if ( !strcpy( target.getName(), &quot;Gorn&quot; ) ) {
               target.life -= dmg;

    }
}
</code></pre>
<p>Hab den Code jetzt nicht geprüft! <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>
<p>Gruß<br />
tHOMY</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321803</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321803</guid><dc:creator><![CDATA[Thomy]]></dc:creator><pubDate>Mon, 09 Jul 2007 19:30:28 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 19:39:38 GMT]]></title><description><![CDATA[<p>nene, nich strcpy <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="😃"
    /> strcmp <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/1321806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321806</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Mon, 09 Jul 2007 19:39:38 GMT</pubDate></item><item><title><![CDATA[Reply to Objekt einer Klasse wird außerhalb von main() nicht mehr erkannt on Mon, 09 Jul 2007 19:40:10 GMT]]></title><description><![CDATA[<blockquote>
<p>wieder schön in die main-Funktion! <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>
</blockquote>
<p>Ahh klappt jetzt, danke.</p>
<blockquote>
<p>Hab den Code jetzt nicht geprüft! <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>
</blockquote>
<p>benutze ich jetzt nicht, klappt ja so eh, trotzdem danke.</p>
<p>OT: [Schleim]Ich wundere mich, wie gut mir hier geholfen wird. In anderen Foren bekäme ich keine solche Antwort und vor allem nicht so schnell. Großes Lob an euch! <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="🙂"
    /> [/Schleim]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1321807</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1321807</guid><dc:creator><![CDATA[Alemarius Nexus]]></dc:creator><pubDate>Mon, 09 Jul 2007 19:40:10 GMT</pubDate></item></channel></rss>