lundi 4 juillet 2016

Overloading operator << in a template class

I'm sorry if this question has been answered allready but I have a read a lot of questions considering overloading this operator in a template class but I haven't found a particular case like mine.

This is my code:

#include <iostream>

using std::cout; 
using std::endl;
using std::ostream;

template <typename T>
class Class
{
    T x;
    public:
    friend ostream& operator << (const Class<T>&, ostream& out);
};

template <typename T>
ostream& operator << (const Class<T>&, ostream& out)
{
    return (out << out.x << endl);
}

int main()
{
    Class<short> object;
    cout << object << endl;
}

And I get this error on last line: Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Class' (or there is no acceptable conversion) g:ucenjeobjektno orijentirano programiranjetemplate3template3main.cpp 25 1 template3

Aucun commentaire:

Enregistrer un commentaire