<?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[Problem mit Konstruktor]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe irgendwie ein Problem mit meinem Konstruktor.</p>
<p>Ich habe die Oberklasse &quot;Studenten&quot; und die Unterklassen &quot;Bachelor&quot; und &quot;Kosi&quot;</p>
<p>Ich habe den Kosi Konstruktor</p>
<pre><code class="language-cpp">Kosi(name, firma)
</code></pre>
<p>und den Bachelor Konstruktor</p>
<pre><code class="language-cpp">Bachelor(name)
</code></pre>
<p>Inhaltlich sind beide identisch, bis auf die Tatsache, dass mein Kosi noch ein Firmenname gespeichert wird.</p>
<p>Nun zum Problem...</p>
<p>rufe ich auf</p>
<pre><code class="language-cpp">Kosi(name, firma)
</code></pre>
<p>funktioniert alles einwandfrei.</p>
<p>Rufe ich allerdings</p>
<pre><code class="language-cpp">Bachelor(name)
</code></pre>
<p>auf, dann wird irgendwie aus einem mir nicht nachvollziehbarem Grund</p>
<pre><code class="language-cpp">Bachelor(void)
</code></pre>
<p>aufgerufen und somit wird nichts gespeichert.</p>
<p>Woran kann das liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/233249/problem-mit-konstruktor</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 21:59:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/233249.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 02 Feb 2009 16:39:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 16:39:39 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe irgendwie ein Problem mit meinem Konstruktor.</p>
<p>Ich habe die Oberklasse &quot;Studenten&quot; und die Unterklassen &quot;Bachelor&quot; und &quot;Kosi&quot;</p>
<p>Ich habe den Kosi Konstruktor</p>
<pre><code class="language-cpp">Kosi(name, firma)
</code></pre>
<p>und den Bachelor Konstruktor</p>
<pre><code class="language-cpp">Bachelor(name)
</code></pre>
<p>Inhaltlich sind beide identisch, bis auf die Tatsache, dass mein Kosi noch ein Firmenname gespeichert wird.</p>
<p>Nun zum Problem...</p>
<p>rufe ich auf</p>
<pre><code class="language-cpp">Kosi(name, firma)
</code></pre>
<p>funktioniert alles einwandfrei.</p>
<p>Rufe ich allerdings</p>
<pre><code class="language-cpp">Bachelor(name)
</code></pre>
<p>auf, dann wird irgendwie aus einem mir nicht nachvollziehbarem Grund</p>
<pre><code class="language-cpp">Bachelor(void)
</code></pre>
<p>aufgerufen und somit wird nichts gespeichert.</p>
<p>Woran kann das liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656661</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656661</guid><dc:creator><![CDATA[skizZ]]></dc:creator><pubDate>Mon, 02 Feb 2009 16:39:39 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 16:47:31 GMT]]></title><description><![CDATA[<p>Vielleicht zeigst du mal deine Konstruktoren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656670</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656670</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 02 Feb 2009 16:47:31 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 17:11:14 GMT]]></title><description><![CDATA[<p>skizZ schrieb:</p>
<blockquote>
<p>Rufe ich allerdings</p>
<pre><code class="language-cpp">Bachelor(name)
</code></pre>
<p>auf, dann wird irgendwie aus einem mir nicht nachvollziehbarem Grund</p>
<pre><code class="language-cpp">Bachelor(void)
</code></pre>
<p>aufgerufen und somit wird nichts gespeichert.</p>
</blockquote>
<p>Zeig mal bitte den Konstruktoraufruf und die Signatur deiner Bachelor-Konstruktoren.</p>
<p>Den C++ kann keinen Konstruktor ohne Parameter aufrufen, wenn Parameter übergeben werden (Dies würde zu einem Compilerfehler führen).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656689</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656689</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Mon, 02 Feb 2009 17:11:14 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 19:01:43 GMT]]></title><description><![CDATA[<p>Hi, ich poste einfach mal meinen Code...</p>
<p>Student.h</p>
<pre><code class="language-cpp">#pragma once
#include &lt;string&gt;
#include &lt;vector&gt;
using namespace std;

class Student
{
protected:
  vector&lt;Student&gt; Studenten;
  string Name;
  string Firmenname;
  int Matrikelnr;
  std::vector&lt;int&gt; Pruefung;
public: 
  Student(void);
  ~Student(void);
  Student(const string&amp;);
  Student(const string&amp;, const string&amp;);
  friend class Kosi;
  friend class Bachelor;
  friend class Manager;
};
</code></pre>
<p>Student.cpp</p>
<pre><code class="language-cpp">#include &quot;Student.h&quot;

extern Student s;

Student::Student(void)
{
}

Student::~Student(void)
{
}

Student::Student(const string&amp; Name)
{
  this-&gt;Name = Name;
}

Student::Student(const string&amp; Name, const string&amp; Firmenname)
{
  this-&gt;Name = Name;
  this-&gt;Firmenname = Firmenname;
}
</code></pre>
<p>Manager.cpp</p>
<pre><code class="language-cpp">#include &quot;Manager.h&quot;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;

extern Student s;

Manager::Manager(void)
{
  using namespace std;
  ifstream namen_dat( &quot;Namen.dat&quot;, ios_base::binary );
  const int N = 30;
  char name_carray[N+1];
  name_carray[N] = 0; // sicheres End-Of-String
  while( namen_dat.read( name_carray, N ) ) // einen Datensatz lesen
  {
    string name = name_carray;
	int rnd=rand()%100;
	if(rnd &gt; 29)
	  Bachelor(name);
    if(rnd&lt;30) 
	{
	  ifstream file;
      int zeile = 0;
      string fileName = &quot;Firmen.txt&quot;;
      file.open(fileName.c_str());
      if(file)
      {
        string text; // Haupttext
        int zeile=rand()%37;
		int zeilen = 0;
        while(!file.eof())
        {
          string txt; // Buffer
          getline(file, txt);
          text = txt + &quot;\n&quot;;
          zeilen++;
          if(zeile == zeilen) // gesuchte Zeile erreicht
          {
            Kosi(name, text);
            break; // Zeile gefunden, Einlesen abbrechen
          }
        }
        file.close();
      }
    }
  }
  cout &lt;&lt; s.Studenten.size();
}

Manager::~Manager(void)
{
}
</code></pre>
<p>Kosi.h</p>
<pre><code class="language-cpp">#pragma once
#include &quot;Student.h&quot;
#include &lt;string&gt;
using namespace std;

class Kosi : public Student
{
private:
  string Firmenname;
public:
  Kosi(void);
  Kosi(const string&amp;, const string&amp;);
  ~Kosi(void);
};
</code></pre>
<p>Kosi.cpp</p>
<pre><code class="language-cpp">#include &quot;Kosi.h&quot;

extern Student s;

Kosi::Kosi(void)
{
}

Kosi::~Kosi(void)
{
}

Kosi::Kosi(const string&amp; Name, const string&amp; Firmenname)
: Student(Name, Firmenname)
{
  s.Studenten.push_back(*this);
}
</code></pre>
<p>Bachelor.h</p>
<pre><code class="language-cpp">#pragma once
#include &lt;string&gt;
#include &quot;Student.h&quot;
using namespace std;

class Bachelor : public Student
{
private:
public:
  Bachelor(void);
  Bachelor(const string&amp;);
  ~Bachelor(void);
};
</code></pre>
<p>Bachelor.cpp</p>
<pre><code class="language-cpp">#include &quot;Bachelor.h&quot;

extern Student s;

Bachelor::Bachelor(void)
{
}

Bachelor::~Bachelor(void)
{
}

Bachelor::Bachelor(const string&amp; Name)
: Student(Name)
{
  s.Studenten.push_back(*this);
}
</code></pre>
<p>Ziel der Übung: Daten aus Dateien zufallsgesteuert zusammenfügen.<br />
Mit dem Kosi funktioniert alles einwandfrei, nur mit dem Bachelor nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656783</guid><dc:creator><![CDATA[skizZ]]></dc:creator><pubDate>Mon, 02 Feb 2009 19:01:43 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 19:17:13 GMT]]></title><description><![CDATA[<p>Was passiert denn genau, wenn du den <code>Bachelor</code> -Konstruktor aufrufst? Geh doch mal mit dem Debugger durch und schau, wo nicht das passiert, das du erwartest. Beim Code sind mir zudem einige Dinge aufgefallen:</p>
<pre><code class="language-cpp">vector&lt;Student&gt; Studenten;
</code></pre>
<p>Innerhalb der <code>Student</code> -Klasse ist es eigentlich nicht sinnvoll, einen Container vom eigenen Typ zu haben. Vielleicht ein Container aus Zeigern, aber auch da stellt sich die Frage, ob der Student die anderen Studenten kennen soll.</p>
<p>Die <code>friend</code> -Deklarationen solltest du wenn möglich vermeiden, da dadurch das Prinzip der Kapselung und dem Schutz der Interna umgangen wird. Wenn du <code>protected</code> -Member hast, können die abgeleiteten Klassen auch so zugreifen.</p>
<pre><code class="language-cpp">extern Student s;
</code></pre>
<p>Was ist mit dem? Wird der irgendwann definiert? Wieso überhaupt global?</p>
<p>Es gibt noch einige weitere Dinge, aber das sollte fürs erste reichen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656798</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656798</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 02 Feb 2009 19:17:13 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 19:29:10 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>alles klar, ich werde das alles mal beachten und nochmal durchgehen. Das mit dem Vektor ist natürlich wahr, den sollte ich in die Managerklasse packen <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=":-/"
      alt="😕"
    /></p>
<p>Also wenn ich den Bachelor aufrufe, ruft er Bachelor(void) auf <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>Ich melde mich morgen nochmal, falls ich es nicht schaffen sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656803</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656803</guid><dc:creator><![CDATA[skizZ]]></dc:creator><pubDate>Mon, 02 Feb 2009 19:29:10 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Mon, 02 Feb 2009 19:30:54 GMT]]></title><description><![CDATA[<p>Probier dein Problem auf etwas einfacheres zu abstrahieren. Das hat viel zu viel drin, was nichts zur Sache tut. Und wenn du das erst mal draussen hast, wirst du den Fehler sicher selber finden.</p>
<p>btw:<br />
- using namespace im Header i.d.R böse.<br />
- Initialisierungsliste benutzen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1656805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1656805</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 02 Feb 2009 19:30:54 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Wed, 04 Feb 2009 20:13:06 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>habe es nun hinbekommen.<br />
allerdings habe ich nun noch eine Frage.</p>
<p>In der Managerklasse habe ich folgenden Container in dem ich alle Studenten sammele</p>
<pre><code class="language-cpp">vector&lt;Student*&gt; Studenten;
</code></pre>
<p>Die Unterklasse Kosi hat als Attribute Name und Firme.<br />
Die Unterklasse Bachelor hat als Attribut nur den Namen.<br />
Die Superklasse Student hat als Attribut nur den Namen.</p>
<p>So, wie kann ich nun von der Managerklasse aus abfragen, welcher gespeicherte Student ein Kosi ist bzw den Firmennamen ausgeben?</p>
<p>Speichern tue ich sie auf diese Art</p>
<pre><code class="language-cpp">Studenten.push_back(new Kosi(name, firma));

Kosi::Kosi(const string&amp; Name, const string&amp; Firmenname)
: Student(Name)
{
  this-&gt;Firmenname = Firmenname;
}

Student::Student(const string&amp; Name)
{
  this-&gt;Name = Name;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1658132</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1658132</guid><dc:creator><![CDATA[skizZ]]></dc:creator><pubDate>Wed, 04 Feb 2009 20:13:06 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Wed, 04 Feb 2009 20:20:15 GMT]]></title><description><![CDATA[<p>Das würde jetzt nur über einen dynamic_cast gehen.<br />
also</p>
<pre><code class="language-cpp">Kosi* test = dynamic_cast&lt;Kosi*&gt;(Studenten[0]);
if( test != 0) // dann ist es ein Kosi
{
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1658138</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1658138</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 04 Feb 2009 20:20:15 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Wed, 04 Feb 2009 20:29:21 GMT]]></title><description><![CDATA[<p>Allerdings kann ich damit noch immer nicht auf das Kosi Attribut Firmenname zugreifen <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1658146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1658146</guid><dc:creator><![CDATA[skizZ]]></dc:creator><pubDate>Wed, 04 Feb 2009 20:29:21 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Konstruktor on Wed, 04 Feb 2009 20:57:21 GMT]]></title><description><![CDATA[<p>skizZ schrieb:</p>
<blockquote>
<p>Allerdings kann ich damit noch immer nicht auf das Kosi Attribut Firmenname zugreifen <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>Doch, kannst du, indem du den Zeiger dereferenzierst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1658156</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1658156</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 04 Feb 2009 20:57:21 GMT</pubDate></item></channel></rss>