I have a class that looks like this.
template<class T> class DataType {
public:
DataType(T v) : value(v) { }
T getValue() { return value; }
ostream & operator<<(ostream &os);
T operator+(DataType<T> &d);
void operator=(DataType<T> &d);
private:
T value;
};
Everything is working except the ostream function. I want to be able to use it with cout. The function is defined like this:
template<class T> ostream & DataType<T>::operator<<(ostream &os) {
os << value;
return os;
}
It compiles successfully when I leave it like this without actually using cout, but when I use cout together with any instance of this class, error messages like the following appears.
no match for 'operator<<' in 'std::cout << length'
candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
What is wrong? The only thing I can think of is that the ostream function must be a normal function used as a friend in the class, because that is the way I did it last time when it worked. I can't find any way to do that when I use templates though.