I was testing a code from Accelerated C++ and (as usual) came up with some errors. The fact is I don't get what to correct in this peace of code. The code is
#include <string>
#include <algorithm>
#include "space.h"
usingnamespace std;
template <class Out> // changed
void split(const string& str, Out os) { // changed
typedef string::const_iterator iter;
iter i = str.begin();
while (i != str.end()) {
// ignore leading blanks
i = find_if(i, str.end(), not_space);
// find end of next word
iter j = find_if(i, str.end(), space);
// copy the characters in [i, j)
if (i != str.end())
*os++ = string(i, j); // changed
i = j;
}
}
and for space.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <cctype> //for isspace()
// true if the argument is whitespace, false otherwise
bool space(char c)
{
return isspace(c);
}
// false if the argument is whitespace, true otherwise
bool not_space(char c)
{
return !isspace(c);
}
OK I don't know a lot about Template functions but I don't know what to search for in here. I figure out that ostream_iterator<string>(cout, "\n") is valid because I used it alone and compiled fine. So the problem should be in split but how do I spot it (and correct it)? I even tried a different string iterator as a second argument in the function split but also an error message occurred.