Wie kann ich eine map mit Strings als Schlüssel und ostream als Wert?

Ich versuche, mit der map container in C++ in der folgenden Weise: Der Schlüssel ist eine string ist und der Wert ein Objekt vom Typ ofstream. Mein code sieht wie folgt aus:

#include <string>
#include <iostream>
#include <map>
#include <fstream>

using namespace std;

int main()
{
  //typedef map<string, int> mapType2;
  //map<string, int> foo;

  typedef map<string, ofstream> mapType;
  map<string, ofstream> fooMap;

  ofstream foo1;
  ofstream foo2; 

  fooMap["file1"] = foo1;
  fooMap["file2"] = foo2;

  mapType::iterator iter = fooMap.begin();
  cout<< "Key = " <<iter->first;
}

Jedoch, wenn ich versuche zu kompilieren Sie den oben aufgeführten code bekomme ich folgende Fehlermeldung:

C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/ios_base.h:
In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)': 
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/ios_base.h:741:
error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
hash.cpp:88: error: within this context

Was mache ich falsch? Wenn dies nicht geschehen kann, mit map, gibt es eine andere Möglichkeit zu schaffen, solche Schlüssel /Wert-paar?

Hinweis: Wenn ich mein test-code mit map<string, int> foo; es funktioniert gut.

Als Stil beiseite, können Sie festlegen, fooMap als Kartentyp z.B. mapType fooMap; da haben Sie schon typedefed.

InformationsquelleAutor apt | 2009-11-30

Schreibe einen Kommentar