Problem in Template function. Uncomprehensive error message

Hello again,

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <iterator>
#include "split.h"

using namespace std;

int main()
{
    string s;
    while (getline(cin, s))
    split(s, ostream_iterator<string>(cout, "\n"));
    return 0;
}


with split.cpp being:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <algorithm>
#include "space.h"

using namespace 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);
}


The error message is
undefined reference to `void split<std::ostream_iterator<std::basic_string<char, std::char_traits<char>,
 std::allocator<char> >, char, std::char_traits<char> > (std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, 
std::ostream_iterator<std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, char, std::char_traits<char> >)'|
.

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.

Any clue about this problem?
are you saving split and space as .cpp files instead of .h files?
No I use also header files (I didn't mention) as well as space.cpp and split.cpp:

split.h:

1
2
3
4
5
6
7
#ifndef GUARD_split_2_h
#define GUARD_split_2_h

template <class Out> // changed
void split(const std::string& str, Out os);

#endif 


And space.h:

1
2
3
4
5
#ifndef GUARD_space_h
#define GUARD_space_h
bool space(char c);
bool not_space(char c);
#endif 


Topic archived. No new replies allowed.