Hi all. I'm starting to learn a bit about template functions and I have a question regarding declaration in a header file.
My code is as follows (from "Accelerated C++, chapter 8"):
#include "Split_Template_func.h"
#include <iostream>
#include <iterator>
using std::cout; using std::cin; using std::string;
using std::getline; using std::ostream_iterator;
int main()
{
string s;
while (getline(cin, s))
split(s, ostream_iterator<string>(cout, "\n"));
return 0;
}
Split_Template_func.cpp:
#include "Split_Template_func.h"
#include <algorithm>
#include <iterator>
#include <cctype>
using std::string; using std::ostream_iterator; using std::find_if;
using std::isspace;
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
template <class Out>
void split(const string& str, Out os)
{
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);
i = j;
}
}
Split_Template_func.h:
#ifndef GUARD_Split_Template_func_h
#define GUARD_Split_Template_func_h
#include <string>
bool space(char);
bool not_space(char);
template <class Out>
void split(const std::string&, Out);
#endif
I am getting an error on line 13 in main:
"undefined reference to 'void split<std::ostream_iterator<std::string, ..."
I assume it is because I have declared the template function incorrectly. How should I have done it?
Thanks.