我想超载'+'
运营商类模板,但得到解析外部符号错误使用中缀符号调用它:解析的外部符号错误重载运算符+模板
// 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年
'地图B运营商+(B常量和LHS,B常量'是不是一个模板声明,但是你定义一个 – LogicStuff
还HTTP看到: //stackoverflow.com/questions/11864102/why-is-friend-member-function-not-recognized-as-function-template-automatically –
顺便说一句,目前所有的成员都是“公共”,所以不需要“朋友”; - ) – Jarod42