<?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[Initialisierung eines Objektvektors über Objektkonstruktor]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mich beim Design eines Programms etwas vertan und stehe jetzt vor dem Problem, dass ich einen vector von Objekten habe, die ich per Konstruktor initialsieren will.</p>
<p>Der Code schaut in etwa so aus:<br />
Klassendefinition:</p>
<pre><code class="language-cpp">class myObj 
{
  private:
    // einige variablen

  public: 
    myObj(string _val) : var(_val) 
    {
       // öffne Datei mit Namen var,
       // lese einige Variablen aus, 
       // berechne einige andere Variablen
       // schließe Datei
    }
};
</code></pre>
<pre><code class="language-cpp">#include &lt;vector&gt;

main()
{
  vector&lt;myObj&gt; Data

  for(unsigned int i = 0; i &lt; 100; ++i)
  {
    Data.push_back(???); // initialisiere Data mit Dateinamen $i
  }

}
</code></pre>
<p>Ich stehe gerade auf dem Schlauch. Gibts da ne Möglichkeit oder geht das voll gegen den Baum?</p>
<p>Gruß,<br />
HarryKane</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/224842/initialisierung-eines-objektvektors-über-objektkonstruktor</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 03:29:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224842.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 15 Oct 2008 17:36:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Wed, 15 Oct 2008 17:36:39 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe mich beim Design eines Programms etwas vertan und stehe jetzt vor dem Problem, dass ich einen vector von Objekten habe, die ich per Konstruktor initialsieren will.</p>
<p>Der Code schaut in etwa so aus:<br />
Klassendefinition:</p>
<pre><code class="language-cpp">class myObj 
{
  private:
    // einige variablen

  public: 
    myObj(string _val) : var(_val) 
    {
       // öffne Datei mit Namen var,
       // lese einige Variablen aus, 
       // berechne einige andere Variablen
       // schließe Datei
    }
};
</code></pre>
<pre><code class="language-cpp">#include &lt;vector&gt;

main()
{
  vector&lt;myObj&gt; Data

  for(unsigned int i = 0; i &lt; 100; ++i)
  {
    Data.push_back(???); // initialisiere Data mit Dateinamen $i
  }

}
</code></pre>
<p>Ich stehe gerade auf dem Schlauch. Gibts da ne Möglichkeit oder geht das voll gegen den Baum?</p>
<p>Gruß,<br />
HarryKane</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599551</guid><dc:creator><![CDATA[harrykane]]></dc:creator><pubDate>Wed, 15 Oct 2008 17:36:39 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Wed, 15 Oct 2008 18:08:40 GMT]]></title><description><![CDATA[<p>So in etwa?</p>
<pre><code class="language-cpp">vector&lt;myObj&gt; Data

stringstream Stream;
string Str;

  for(unsigned int i = 0; i &lt; 100; ++i)
  {
    Stream.str(&quot;&quot;); //Stream leeren
    Stream&lt;&lt;i;      //i hineinschreiben
    Str= Stream.str(); //Den Inhalt in Str schreiben, damit wird zb aus i=3 Str=&quot;3&quot;

    Data.push_back(myObj(Str)); // initialisiere Data mit Dateinamen $i
  }
</code></pre>
<p>Was ist dein Problem?<br />
Die konvertierung von int nach string oder das Initialisieren in während des push_back?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599566</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599566</guid><dc:creator><![CDATA[Blaze]]></dc:creator><pubDate>Wed, 15 Oct 2008 18:08:40 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Wed, 15 Oct 2008 18:10:22 GMT]]></title><description><![CDATA[<p>Wo liegt das Problem?</p>
<pre><code class="language-cpp">Data.push_back(myObj(/* Argumentliste für Konstruktor */));
</code></pre>
<p>Im Weiteren muss hat <code>main()</code> den Rückgabetyp <code>int</code> , was in C++ explizit angegeben werden muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599568</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599568</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 15 Oct 2008 18:10:22 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 08:44:58 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Wo liegt das Problem?</p>
<pre><code class="language-cpp">Data.push_back(myObj(/* Argumentliste für Konstruktor */));
</code></pre>
<p>Im Weiteren muss hat <code>main()</code> den Rückgabetyp <code>int</code> , was in C++ explizit angegeben werden muss.</p>
</blockquote>
<p>Öhm ... ja ... man bin ich blöd. Ok, danke ... hab den Wald vor lauter Bäumen nicht gesehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599781</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599781</guid><dc:creator><![CDATA[harrykane]]></dc:creator><pubDate>Thu, 16 Oct 2008 08:44:58 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 08:53:49 GMT]]></title><description><![CDATA[<p>Geht nicht...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599783</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Thu, 16 Oct 2008 08:53:49 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 09:16:52 GMT]]></title><description><![CDATA[<p>Ok, also mein erstes Problem wurde gelöst, ich war wohl gestern etwas neben der Spur. Doch nun hat sich ein anderes Problem aufgetan (ich hoffe das lässt sich ebenso einfach lösen <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>Das Problem liegt wohl beim ifstream im Objekt. Wenn ich den ifstream in den Konstruktor packe ist es wohl kein Problem (zumindest meckert er dann nicht).</p>
<p>Kann mir bitte jemand erklären (für einen Volltrottel wie mich) warum das so nicht geht und was ich anders machen muss?</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;fstream&gt;

class myObj 
{
  private:
    ifstream file;
    // einige variablen

  public: 
    myObj(string _val) : var(_val) 
    {
      file.open(var.c_str());
      // lese einige Variablen aus der Datei, 
      // berechne einige andere Variablen
      // schließe Datei
      file.close();
    }
};
</code></pre>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;vector&gt;

int main()
{
  vector&lt;myObj&gt; Data

  string filename = &quot;dateiname.txt&quot;
  Data.push_back(myObj(filename));

 return 0;
}
</code></pre>
<p>Fehlermeldung: (ich hoffe, es verwirrt niemanden zusehr, dass myObj hier TEST heisst).</p>
<pre><code>test.cc:96: note: synthesized method ‘TEST::TEST(const TEST&amp;)’ first required here
/usr/include/c++/4.1.2/bits/ios_base.h: In member function ‘std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’:
/usr/include/c++/4.1.2/iosfwd:55:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::_M_insert_aux(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, const _Tp&amp;) [with _Tp = TEST, _Alloc = std::allocator&lt;TEST&gt;]’
/usr/include/c++/4.1.2/bits/stl_vector.h:610:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::push_back(const _Tp&amp;) [with _Tp = TEST, _Alloc = std::allocator&lt;TEST&gt;]’
test.cc:96:   instantiated from here
/usr/include/c++/4.1.2/bits/ios_base.h:782: error: ‘std::ios_base&amp; std::ios_base::operator=(const std::ios_base&amp;)’ is private
/usr/include/c++/4.1.2/iosfwd:55: error: within this context
/usr/include/c++/4.1.2/iosfwd: In member function ‘std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’:
/usr/include/c++/4.1.2/iosfwd:61: note: synthesized method ‘std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_ios&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’ first required here
/usr/include/c++/4.1.2/iosfwd: In member function ‘std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’:
/usr/include/c++/4.1.2/iosfwd:89: note: synthesized method ‘std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’ first required here
/usr/include/c++/4.1.2/streambuf: In member function ‘std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’:
/usr/include/c++/4.1.2/streambuf:789: error: ‘std::basic_streambuf&lt;_CharT, _Traits&gt;&amp; std::basic_streambuf&lt;_CharT, _Traits&gt;::operator=(const std::basic_streambuf&lt;_CharT, _Traits&gt;&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]’ is private
/usr/include/c++/4.1.2/iosfwd:86: error: within this context
/usr/include/c++/4.1.2/iosfwd: In member function ‘std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’:
/usr/include/c++/4.1.2/iosfwd:89:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::_M_insert_aux(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, const _Tp&amp;) [with _Tp = TEST, _Alloc = std::allocator&lt;TEST&gt;]’
/usr/include/c++/4.1.2/bits/stl_vector.h:610:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::push_back(const _Tp&amp;) [with _Tp = TEST, _Alloc = std::allocator&lt;TEST&gt;]’
test.cc:96:   instantiated from here
/usr/include/c++/4.1.2/iosfwd:89: note: synthesized method ‘std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’ first required here
test.cc: In member function ‘TEST&amp; TEST::operator=(const TEST&amp;)’:
test.cc:11: note: synthesized method ‘std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;::operator=(const std::basic_ifstream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)’ first required here
/usr/include/c++/4.1.2/bits/vector.tcc: In member function ‘void std::vector&lt;_Tp, _Alloc&gt;::_M_insert_aux(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, const _Tp&amp;) [with _Tp = TEST, _Alloc = std::allocator&lt;TEST&gt;]’:
/usr/include/c++/4.1.2/bits/stl_vector.h:610:   instantiated from ‘void std::vector&lt;_Tp, _Alloc&gt;::push_back(const _Tp&amp;) [with _Tp = TEST, _Alloc = std::allocator&lt;TEST&gt;]’
test.cc:96:   instantiated from here
/usr/include/c++/4.1.2/bits/vector.tcc:260: note: synthesized method ‘TEST&amp; TEST::operator=(const TEST&amp;)’ first required here
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1599795</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599795</guid><dc:creator><![CDATA[harrykane]]></dc:creator><pubDate>Thu, 16 Oct 2008 09:16:52 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 09:13:50 GMT]]></title><description><![CDATA[<p>Öhhm. Hast du alles nötige included?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599798</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599798</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 16 Oct 2008 09:13:50 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 09:20:16 GMT]]></title><description><![CDATA[<p>Ja, denke schon (habs oben noch hinzugefügt).</p>
<p>Ich habe festgestellt, wenn ich ifstream in den Konstrukter schiebe, also</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;fstream&gt;

class myObj 
{
  private:
    // einige variablen

  public: 
    myObj(string _val) : var(_val) 
    {
      ifstream file;

      file.open(var.c_str());
      // lese einige Variablen aus der Datei, 
      // berechne einige andere Variablen
      // schließe Datei
      file.close();
    }
};
</code></pre>
<p>schreibe, scheint es zu funktionieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599803</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599803</guid><dc:creator><![CDATA[harrykane]]></dc:creator><pubDate>Thu, 16 Oct 2008 09:20:16 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 09:20:38 GMT]]></title><description><![CDATA[<p>Ich denke mal, dass der fstream ein Member von Deiner Klasse ist. Streams lassen sich aber nicht kopieren. Du musst also einen Copy-Kosntruktor für Deine Klasse anlegen, und dann den Stream als Referenz weiterleiten, oder noch besser: den stream lokal in der Memberfunktion anlegen.<br />
Also nochmal im Klartext:<br />
Beim push_back wird ein temoräres Objekt erzeugt, und das dann in den Vektor kopiert. Das geht aber so nicht, weil Deine Klasse einen fstream als Member beinhaltet, und streams nicht kopierbar sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599806</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Thu, 16 Oct 2008 09:20:38 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 09:19:49 GMT]]></title><description><![CDATA[<p>push_back verlangt, dass das Objekt kopierbar ist. Deines ist es aber nicht, da du ifstream als Membervariable hast und ifstream ist nicht kopierbar. Mach dein ifstream lokal im Konstruktor und alles sollte gehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1599808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599808</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 16 Oct 2008 09:19:49 GMT</pubDate></item><item><title><![CDATA[Reply to Initialisierung eines Objektvektors über Objektkonstruktor on Thu, 16 Oct 2008 09:21:47 GMT]]></title><description><![CDATA[<p>Ah, danke. Stimmt, ich kann ja genauso in den Memberfunktionen den ifstream deklarieren ...</p>
<p>Ich wusste doch, dass es eine &quot;einfache&quot; Lösung gibt.</p>
<p>Vielen Dank noch mal. <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/1599809</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1599809</guid><dc:creator><![CDATA[harrykane]]></dc:creator><pubDate>Thu, 16 Oct 2008 09:21:47 GMT</pubDate></item></channel></rss>