我正在尝试为类模板重载'+'
运算符,但在使用中缀表示法调用它时出现了未解决的外部符号错误:
// In main.cpp
template<class T>
struct B
{
B(T t) : t_(t) {}
friend B operator+(B const &lhs, B const &rhs);
T t_;
};
template<class T>
B<T> operator+(B<T> const &lhs, B<T> const &rhs)
{
return B<T>(lhs.t_ + rhs.t_);
}
int main()
{
B<int> b = 1;
b = operator+<int>(b, 2); // works but clunky syntax
// b = b + 2; // LNK2019: unresolved external symbol
}
对于常规非模板类很好用,因此想知道能否在这里实现相同的效果。
我正在使用Visual C++ 2015。