Handling map of files in c++
<p>I need to write to a bunch of files simultaneously, so I decided to use <code>map &lt;string, ofstream&gt;</code>.</p>

<pre><code>map&lt;string, ofstream&gt; MyFileMap; 
</code></pre>

<p>I take a <code>vector&lt;string&gt; FileInd</code>, which consists of, say <code>"a" "b" "c"</code>, and try to open my files with:</p>

<pre><code>for (vector&lt;string&gt;::iterator it = FileInd.begin(); iter != FileInd.end(); ++it){
    ...
    MyFileMap[*it].open("/myhomefolder/"+(*it)+".");
}
</code></pre>

<p>I get the error</p>

<pre><code>request for member 'open' in ..... , which is of non-class type 'std::ofstream*'
</code></pre>

<p>I've tried to switch to </p>

<pre><code>map&lt;string, ofstream*&gt; MyFileMap;
</code></pre>

<p>But it didn't work either.</p>

<p>Could anyone help?</p>

<p>Thanks.</p>

<p><strong>Clarification:</strong></p>

<p>I've tried both </p>

<p><code>map&lt;string, ofstream&gt; MyFileMap;</code>
<code>map&lt;string, ofstream*&gt; MyFileMap;</code> </p>

<p>with both</p>

<p><code>.open</code>
<code>-&gt;open</code></p>

<p>neither of 4 variants work.</p>

<p><strong>Solution (suggested in Rob's code below):</strong></p>

<p>Basically, I forgot "new", the following works for me:</p>

<pre><code>map&lt;string, ofstream*&gt; MyFileMap;
MyFileMap[*it] = new ofstream("/myhomefolder/"+(*it)+".");
</code></pre>
