Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未解析的外部符号错误重载运算符+;模板_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 未解析的外部符号错误重载运算符+;模板

C++ 未解析的外部符号错误重载运算符+;模板,c++,templates,operator-overloading,C++,Templates,Operator Overloading,我试图重载类模板的'+'运算符,但使用中缀符号调用它时出现未解决的外部符号错误: // 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> con

我试图重载类模板的
'+'
运算符,但使用中缀符号调用它时出现未解决的外部符号错误:

// 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
}
//在main.cpp中
模板
结构B
{
B(T):T_u(T){}
友元B操作符+(B常量和左侧,B常量和右侧);
T!;
};
模板
B操作员+(B常量和左侧、B常量和右侧)
{
返回B(左S.t_u2;+右S.t_2;);
}
int main()
{
B=1;
b=运算符+(b,2);//有效但语法笨拙
//b=b+2;//LNK2019:未解析的外部符号
}
它适用于常规的非模板类,因此想知道是否有可能在这里实现同样的功能

我使用Visual C++ 2015。< /P>

friend B operator+(B const &lhs, B const &rhs);
是一个非模板函数

更简单的方法是定义它
内联

template<class T>
struct B
{
    B(T t) : t_(t) {}
    friend B operator+(B const &lhs, B const &rhs)
    {
       return B(lhs.t_ + rhs.t_);
    }
    T t_;   
};
模板
结构B
{
B(T):T_u(T){}
友元B运算符+(B常量和左侧、B常量和右侧)
{
返回B(左S.t_u2;+右S.t_2;);
}
T!;
};

否则,您必须声明所有模板

template <typename T> struct B;

template <typename T> B<T> operator+(B<T> const &lhs, B<T> const &rhs);

template<class T>
struct B
{
    B(T t) : t_(t) {}

    template <typename U>
    friend B<U> operator+(B<U> const &lhs, B<U> const &rhs);
    T t_;   
};
模板结构B;
模板B运算符+(B常量和左侧、B常量和右侧);
模板
结构B
{
B(T):T_u(T){}
模板
友元B操作符+(B常量和左侧,B常量和右侧);
T!;
};

或者只是具体的一个

template <typename T> struct B;

template <typename T> B<T> operator+(B<T> const &lhs, B<T> const &rhs);

template<class T>
struct B
{
    B(T t) : t_(t) {}
    friend B operator+<>(B const &lhs, B const &rhs);
    // Note the <>

    T t_;   
};

template<class T>
B<T> operator+(B<T> const &lhs, B<T> const &rhs)
{
    return B<T>(lhs.t_ + rhs.t_);
}
模板结构B;
模板B运算符+(B常量和左侧、B常量和右侧);
模板
结构B
{
B(T):T_u(T){}
友元B操作符+(B常量和左侧,B常量和右侧);
//注意
T!;
};
模板
B操作员+(B常量和左侧、B常量和右侧)
{
返回B(左S.t_u2;+右S.t_2;);
}

您尚未将其定义为类内的模板。 最简单的解决方案是在类内定义函数(内联):

模板
结构B
{
B(T):T_u(T){}
朋友B操作员+(常数B&lhs、常数B&rhs)
{
返回B(左S.t_u2;+右S.t_2;);
}
T!;
};

友元B操作符+(B常量和左侧、B常量和右侧)
不是一个模板声明,但是您定义了一个。另外请参见,目前所有成员都是
公共的
,因此不需要
朋友
;-)理解。谢谢你的帮助。如果有人读了这篇文章,我也觉得很有帮助。@geogeskelton指向关于模板的文章的评论非常好。那篇文章在解释问题/选项方面做得很好,可以理解。定义它内联工作,但我想避免这样做,因为我真正的类里面有很多东西。我发现我没有定义一个模板函数,但是我尝试了你的解决方案,在这两种情况下都得到了
C2678:binary'+':没有找到接受类型为“B”的左操作数的运算符(或者没有可接受的转换)
@GeorgeSkelton:我添加了链接。啊,好的,我明白你的意思了,谢谢。但是它仍然不会编译
b+1
,因为它不能从
int
中推断
b
。然而,成员函数可以进行这样的推断。这只是friend函数的固有限制吗?模板的“限制”。如何知道从
int
生成哪个模板?您知道只有
B
具有正确的构造函数,但它们可能具有专门化,等等。
template<class T>
struct B
{
    B (T t) : t_ (t) {}
    friend B operator+ (const B &lhs, const B &rhs)
    {
       return B (lhs.t_ + rhs.t_);
    }
    T t_;   
};