小编Int*_*rca的帖子

模板运算符重载类头之外的实现

'util.h'中定义的以下代码编译和链接.但是,当我将运算符重载的实现移动到'util.cc'时,链接器无法解析符号.这可能是这样做,还是由于模板的性质而无法做到这一点?

谢谢,


工作

util.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);
  }
};
Run Code Online (Sandbox Code Playgroud)

不工作

util.h

template<class T>
struct Rect {
  T x, y, w, h;

  friend bool operator ==(const Rect<T> &a, const Rect<T> &b);
  friend bool …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading friend

5
推荐指数
1
解决办法
3330
查看次数

标签 统计

c++ ×1

friend ×1

operator-overloading ×1

templates ×1