'util.h' 中定义的以下代码编译和链接。但是,当我将运算符重载的实现移到“util.cc”中时,链接器无法解析符号。这是可能的,还是由于模板的性质而不能这样做?
谢谢,
在职的
实用程序.h
template<class T>
struct Rect {
T x, y, w, h;
friend bool operator ==(const Rect<T> &a, const Rect<T> &b) {
return (a.x == b.x && a.y == b.y && a.w == b.w && a.h == b.h);
}
friend bool operator !=(const Rect<T> &a, const Rect<T> &b) {
return !(a == b);
}
};
不工作
实用程序.h
template<class T>
struct Rect {
T x, y, w, h;
friend bool operator ==(const Rect<T> &a, const Rect<T> &b);
friend bool operator !=(const Rect<T> &a, const Rect<T> &b);
};
实用程序.cc
template<class T>
bool operator ==(const Rect<T> &a, const Rect<T> &b)
{
return (a.x == b.x && a.y == b.y && a.w == b.w && a.h == b.h);
}
template<class T>
bool operator !=(const Rect<T> &a, const Rect<T> &b)
{
return !(a == b);
}