class template help

Started by
4 comments, last by superpig 16 years, 11 months ago
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.
Advertisement
From the top of my head: forward declaration of the function template, friend declaration of an instance of that template, and implementation of the template.
Your code fails to compile because member operators only allow the owner object to be placed to their left (and, in the case of streams, is placed to the right).

template <typename T> ostream& operator<<(ostream &, const DataType<T> &);template<class T>class DataType {  public:    DataType(T v) : value(v) { }    T getValue() { return value; }    friend ostream & operator<< <T>(ostream &, const DataType<T> &);        T operator+(DataType<T> &d);    void operator=(DataType<T> &d);           private:    T value;};template <typename T> ostream& operator<<(ostream & o, const DataType<T> & d) {   return o << d.value;}
The error messages disappeared, but another 3 appeared instead.

In instantiation of `DataType<float>':

Line 50: instantiated from here

Line 21: template-id `operator<< <float>' for `std::basic_ostream<char, std::char_traits<char> >& operator<<(std::basic_ostream<char, std::char_traits<char> >&, DataType<float>&)' does not match any template declaration

The lines they are pointing to are the following:
DataType<float> grade(15.04); // this line worked before (line 50)friend ostream & operator<< <T>(ostream &, DataType<T> &); // line 21
I forgot the const on the second member in the friend declaration, sorry about that.
That's not your fault, I removed the const because I got an error message complaining about it. passing `const DataType<float>' as `this' argument of `T DataType<T>::getValue() [with T = float]' discards qualifiers. The error message disappeared when I removed const for some reason.
DataType::getValue() should be marked as const as well (i.e. T getValue() const { return value; }). Then you should be able to reintroduce the original const and not have it produce errors.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement