writing a map to file

I'm using maps and writing files for the first time and I get a crazy compiler error when I try to compile the following code. I'm sure it's something stupid, but I've been staring at this thing for too long, so any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

//map is named schedules
// saveSchedule() is a member of the Schedule class that writes a vector of
//objects to disk, or is supposed to

        ofstream newFile("sched.txt");
        map<string,Schedule>::iterator in;
        for(in=schedules.begin(); in!=schedules.end();in++)
        {
                Schedule saveSched;
                saveSched = in->second;
                newFile<< (in->first)<<" : " << saveSched.saveSchedule()
                        << endl;
        }
        newFile.close();
Mind reading costs extra. What compiler error are you getting?
date_driver.cpp:218:20: error: no matching function for call to ‘std::basic_ofstream<char>::write(std::pair<const std::basic_string<char>, Schedule>&)’

but the above error came after I tried to change it use write(). Still no good. Here's what it looks like now

1
2
3
4
5
6
7
8
9
10
11
12
13
        ofstream newFile("sched.txt");
        map<string,Schedule>::iterator in;
        for(in=schedules.begin(); in!=schedules.end();in++)
        {
                Schedule saveSched;
                saveSched = in->second;
                //newFile<< (in->first)<<" : " << saveSched.saveSchedule()
                //      << endl;
                newFile.write(*in);
                newFile.write(":");
                saveSched.saveSchedule();
                newFile.write("\n");
        }
Last edited on
write() takes two parameters, a character array and an integral value. Looks like your code is trying to send in an iterator to the map. There's no overload of the write() function for that.

http://www.cplusplus.com/reference/ostream/ostream/write/
ostream& write (const char* s, streamsize n);
Write block of data
Inserts the first n characters of the array pointed by s into the stream.
Topic archived. No new replies allowed.