0

I want to create a map of a key and the corresponding file in C++. I used the below snipper, which is giving mean exception at last line *m_jsTabFilesMap[key] << text;:

    std::map<std::string, std::ofstream*> m_jsTabFilesMap;
    std::string text = "hello all";
    std::string key = "a";
    *m_jsTabFilesMap[key] << text;
bfontaine
  • 18,169
  • 13
  • 73
  • 107
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • Already answered here: https://stackoverflow.com/questions/9138727/handling-map-of-files-in-c – Jonaswg Aug 18 '17 at 08:41
  • This is now a totally different question. But anyway, you need to really read about pointers before using them. `*m_jsTabFilesMap[key]` means "Use the `std::ofstream` that is already in `m_jsTabFilesMap[key]`", but you haven't yet put anything there. – Arthur Tacca Aug 18 '17 at 09:07
  • Make sure that the file for your key is assigned to a valid opened stream instance. If you get an exception, **always** include the exception type, message, stacktrace and other relevant information. – grek40 Aug 18 '17 at 09:07

1 Answers1

2

The copy constructor of std::ofstream is explicitely deleted. You have to store the streams by pointer or in a different way that doesn't try to copy the stream object if you want them in the map.

grek40
  • 13,113
  • 1
  • 24
  • 50
  • How do i achieve this ? Can you please provide an example – codeLover Aug 18 '17 at 08:45
  • @codeLover sorry for answering before you formulated an actual question, maybe I did the wrong thing here. Please look at the commented duplicates and update your question to state what exactly you want to do if the linked duplicates don't work for you. – grek40 Aug 18 '17 at 08:49
  • Edited my question to be more specific. – codeLover Aug 18 '17 at 09:04