【发布时间】:2009-11-30 12:41:22
【问题描述】:
我正在尝试通过以下方式在 C++ 中使用map 容器:键是string,值是ofstream 类型的对象。我的代码如下所示:
#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;
}
但是,当我尝试编译上述代码时,出现以下错误:
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
出了什么问题?如果使用map 无法做到这一点,是否有其他方法可以创建这样的键:值对?
注意:如果我使用 map<string, int> foo; 测试我的代码,它可以正常工作。
【问题讨论】:
-
作为一种风格,您可以将 fooMap 定义为 mapType 例如地图类型 fooMap;因为你已经定义了它。
标签: c++ dictionary stdmap ostream