I try to implement the following function :
template<typename T>
class a
{
private:
T var;
friend bool operator==(const a<T> &, const a<T> &);
};
template<typename T> inline bool operator==(const a<T> &r1, const a<T> &r2)
{
return r1.var==r2.var;
}
int main () {
a<int> var0;
a<int> var1;
var0 == var1;
}
However, I get
main.obj : error LNK2001: unresolved external symbol "bool __cdecl operator==(class a<int> const &,class a<int> const &)" (??8@YA_NABV?$a@H@@0@Z)
under VC++ 2008
May I know how I can fix the linking error?
What you have declares the friend op== as a non-template, but you implement it as a template. That is why the definition is not found when linking.
How I usually overload op== for class templates: