如何设置以 string 作为键、ostream 作为值的映射?

发布于 2024-08-12 16:31:12 字数 1346 浏览 8 评论 0原文

我尝试按以下方式在 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测试我的代码; foo; 它工作正常。

I am trying to use the map container in C++ in the following way: The Key is a string and the value is an object of type ofstream. My code looks as follows:

#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;
}

However, when I try to compile the above code, I get the following error:

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

What is going wrong? If this cannot be done using map, is there some other way to create such key:value pair?

Note: If I test my code with map<string, int> foo; it works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光沙漏 2024-08-19 16:31:12

流不喜欢被复制。最简单的解决方案是使用指向映射中流的指针(或者更好的是智能指针):

typedef map<string, ofstream*> mapType;

Streams do not like being copied. The simplest solution is using a pointer (or better, a smart pointer) to a stream in the map:

typedef map<string, ofstream*> mapType;
新雨望断虹 2024-08-19 16:31:12

ofstream 类型的对象不可复制,这是放入任何标准库容器的先决条件。

Objects of type ofstream aren't copiable, which is a precondition to be put into any Standard Library container.

謌踐踏愛綪 2024-08-19 16:31:12

operator=std::ios_base 私有的,ofstream 是从中派生的。因此您无法复制对象 foo1foo2

The operator= is private for std::ios_base, from which ofstream is derived. So you can't copy the objects foo1 and foo2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文