String이 Key 및 Ostream으로 값으로 맵을 설정하려면 어떻게해야합니까?

StackOverflow https://stackoverflow.com/questions/1819603

문제

나는 사용하려고한다 map C ++의 컨테이너는 다음과 같은 방식으로 : 키는 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; 잘 작동합니다.

도움이 되었습니까?

해결책

스트림은 복사하는 것을 좋아하지 않습니다. 가장 간단한 솔루션은지도의 스트림에 포인터 (또는 더 나은 스마트 포인터)를 사용하는 것입니다.

typedef map<string, ofstream*> mapType;

다른 팁

유형의 대상 ofstream 복사 가능하지 않으며 모든 표준 라이브러리 컨테이너에 넣을 전제 조건입니다.

그만큼 operator= 비공개입니다 std::ios_base, 어떤에서 ofstream 파생. 따라서 객체를 복사 할 수 없습니다 foo1 그리고 foo2.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top