Possible Duplicate:
overloading friend operator<< for template class
I'm trying to overload operator<< for a template class but I'm getting errors...
Final(fixed) code:
template<class T>
class mytype
{
T atr;
public:
mytype();
mytype(T);
mytype(mytype&);
T getAtr() const;
T& operator=(const T&);
template<class U> friend ostream& operator<<(ostream&,const mytype<U>&);
};
template<class T>
mytype<T>::mytype()
{
atr=0;
}
template<class T>
mytype<T>::mytype(T value)
{
atr=value;
}
template<class T>
mytype<T>::mytype(mytype& obj)
{
atr=obj.getAtr();
}
template<class T>
T mytype<T>::getAtr() const
{
return atr;
}
template<class T>
T& mytype<T>::operator=(const T &other)
{
atr=other.getAtr();
return *this;
}
template<class U>
ostream& operator<<(ostream& out,const mytype<U> &obj)
{
out<<obj.getAtr();
return out;
}
(All in the header file)
VS2012 errors:
1)
Error 1 error LNK2019: unresolved external symbol "public: __thiscall mytype::mytype(int)" (??0?$mytype@H@@QAE@H@Z) referenced in function _wmain
2)
Error 2 error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class mytype const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$mytype@H@@@Z) referenced in function _wmain
3)
Error 3 error LNK1120: 2 unresolved externals
What's wrong with my code?