<?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[Methodendefintion außerhalb der Klasse]]></title><description><![CDATA[<p>Hallo,</p>
<p>im Land von C++ bin ich noch relativ neu. Vorher habe ich v.A. mit Java programmiert und so manch C++-spezifisches Programmierkonzept erzeugt bei mir noch ein großes Fragezeichen.</p>
<p>Ich arbeite mit Dev-Cpp und habe ein Projekt mit den beiden Dateien &quot;TEILNEHMER.h&quot; und &quot;main.cpp&quot; erzeugt. Außerhalb der Klasse in der TEILNEHMER.h-Datei versuche ich eine Methode zu definieren. Interessanterweiße hat Dev-Cpp den Code einige male kompiliert, so wie ich ihn gleich poste. Dann habe ich ein paar Dinge geändert, die ich dann aber wieder komplett rückgängig gemacht habe.</p>
<p>Jetzt wird der Code nicht mehr kompiliert. Könnt ihr mir da weiter helfen? Die interessante Stelle markiere ich gleich mal fett.</p>
<pre><code>class TEILNEHMER
{
      private:
              std::string name;
              int ima;
              static int counter;      

      public:
             TEILNEHMER();
             TEILNEHMER(std::string a, int b);
             ~TEILNEHMER();
             [b]static int getCount();[/b]
             std::string getName();
             int getMtrk(); 

};

  TEILNEHMER::TEILNEHMER():name(&quot;Max Mustermann&quot;),ima(123456789)
  {
   counter++;                          
  }

  TEILNEHMER::TEILNEHMER(std::string a, int b):name(a), ima(b) 
  {
   counter++;                                
  }

  TEILNEHMER::~TEILNEHMER()
  {
   counter--;
   std::cout &lt;&lt; &quot;Teilnehmer gelöscht!&quot;;
   std::cout &lt;&lt; std::endl;
   std::cout &lt;&lt; std::endl;
  }

  static int getCount()
  {
         [b]return counter;[/b]
  }

  std::string TEILNEHMER::getName()
  {
         return name;
  }

  int TEILNEHMER::getMtrk()
  {
         return ima;
  }
</code></pre>
<pre><code>#include &lt;iostream&gt;
#include &quot;TEILNEHMER.H&quot;

using namespace std;

int TEILNEHMER::counter = 0;

int main()
{   
    TEILNEHMER TEST1;       
    TEILNEHMER TEST2(&quot;Hans Wurst&quot;, 21403885);
    cout &lt;&lt; &quot;Anzahl der erzeugten Teilnehmer: &quot;;    
    cout &lt;&lt; TEILNEHMER::getCount();
    cout &lt;&lt; endl;

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
}
</code></pre>
<p>Folgende Fehlermeldungen werden ausgespuckt:</p>
<blockquote>
<ul>
<li>3 In file included from main.cpp</li>
<li>In function `int getCount()':</li>
<li>38 `counter' undeclared (first use this function)</li>
<li>...</li>
</ul>
</blockquote>
<p>Wenn ich die Methode noch in der Klasse definiere, also z.B: so:</p>
<pre><code>static int getCount(){return counter;};
</code></pre>
<p>dann funktioniert es ohne Fehlermeldungen. Aber aus Gründen der Übersichtlichkeit will ich die Methode immer außerhalb der Klasse definieren.<br />
Was läuft hier falsch?</p>
<p>Vielen Dank schonmal für eure Hilfe!</p>
<p>Liebe Grüße,<br />
Oli</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304511/methodendefintion-außerhalb-der-klasse</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 17:37:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304511.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 07 Jun 2012 20:28:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Methodendefintion außerhalb der Klasse on Thu, 07 Jun 2012 20:28:08 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>im Land von C++ bin ich noch relativ neu. Vorher habe ich v.A. mit Java programmiert und so manch C++-spezifisches Programmierkonzept erzeugt bei mir noch ein großes Fragezeichen.</p>
<p>Ich arbeite mit Dev-Cpp und habe ein Projekt mit den beiden Dateien &quot;TEILNEHMER.h&quot; und &quot;main.cpp&quot; erzeugt. Außerhalb der Klasse in der TEILNEHMER.h-Datei versuche ich eine Methode zu definieren. Interessanterweiße hat Dev-Cpp den Code einige male kompiliert, so wie ich ihn gleich poste. Dann habe ich ein paar Dinge geändert, die ich dann aber wieder komplett rückgängig gemacht habe.</p>
<p>Jetzt wird der Code nicht mehr kompiliert. Könnt ihr mir da weiter helfen? Die interessante Stelle markiere ich gleich mal fett.</p>
<pre><code>class TEILNEHMER
{
      private:
              std::string name;
              int ima;
              static int counter;      

      public:
             TEILNEHMER();
             TEILNEHMER(std::string a, int b);
             ~TEILNEHMER();
             [b]static int getCount();[/b]
             std::string getName();
             int getMtrk(); 

};

  TEILNEHMER::TEILNEHMER():name(&quot;Max Mustermann&quot;),ima(123456789)
  {
   counter++;                          
  }

  TEILNEHMER::TEILNEHMER(std::string a, int b):name(a), ima(b) 
  {
   counter++;                                
  }

  TEILNEHMER::~TEILNEHMER()
  {
   counter--;
   std::cout &lt;&lt; &quot;Teilnehmer gelöscht!&quot;;
   std::cout &lt;&lt; std::endl;
   std::cout &lt;&lt; std::endl;
  }

  static int getCount()
  {
         [b]return counter;[/b]
  }

  std::string TEILNEHMER::getName()
  {
         return name;
  }

  int TEILNEHMER::getMtrk()
  {
         return ima;
  }
</code></pre>
<pre><code>#include &lt;iostream&gt;
#include &quot;TEILNEHMER.H&quot;

using namespace std;

int TEILNEHMER::counter = 0;

int main()
{   
    TEILNEHMER TEST1;       
    TEILNEHMER TEST2(&quot;Hans Wurst&quot;, 21403885);
    cout &lt;&lt; &quot;Anzahl der erzeugten Teilnehmer: &quot;;    
    cout &lt;&lt; TEILNEHMER::getCount();
    cout &lt;&lt; endl;

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
}
</code></pre>
<p>Folgende Fehlermeldungen werden ausgespuckt:</p>
<blockquote>
<ul>
<li>3 In file included from main.cpp</li>
<li>In function `int getCount()':</li>
<li>38 `counter' undeclared (first use this function)</li>
<li>...</li>
</ul>
</blockquote>
<p>Wenn ich die Methode noch in der Klasse definiere, also z.B: so:</p>
<pre><code>static int getCount(){return counter;};
</code></pre>
<p>dann funktioniert es ohne Fehlermeldungen. Aber aus Gründen der Übersichtlichkeit will ich die Methode immer außerhalb der Klasse definieren.<br />
Was läuft hier falsch?</p>
<p>Vielen Dank schonmal für eure Hilfe!</p>
<p>Liebe Grüße,<br />
Oli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220763</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220763</guid><dc:creator><![CDATA[lerneLebenslang]]></dc:creator><pubDate>Thu, 07 Jun 2012 20:28:08 GMT</pubDate></item><item><title><![CDATA[Reply to Methodendefintion außerhalb der Klasse on Thu, 07 Jun 2012 20:40:35 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int TEILNEHMER::getCount()
  {
         return counter;
  }
</code></pre>
<p><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/2220769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220769</guid><dc:creator><![CDATA[boost]]></dc:creator><pubDate>Thu, 07 Jun 2012 20:40:35 GMT</pubDate></item><item><title><![CDATA[Reply to Methodendefintion außerhalb der Klasse on Thu, 07 Jun 2012 20:48:14 GMT]]></title><description><![CDATA[<p>Du wirst verrückt... da wäre ich so schnell nicht drauf gekommen.</p>
<p>Ja vielen, vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2220777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2220777</guid><dc:creator><![CDATA[lerneLebenslang]]></dc:creator><pubDate>Thu, 07 Jun 2012 20:48:14 GMT</pubDate></item></channel></rss>