c – 转发模板功能声明
发布时间:2020-12-16 05:03:32 所属栏目:百科 来源:网络整理
导读:我有一个带有朋友模板功能的模板类.我目前有以下代码,它正在工作: templateclass Tclass Vector{ public: templateclass U,class W friend VectorU operator*(const W lhs,const VectorU rhs);}templateclass U,class WVectorU operator*(const W lhs,const
我有一个带有朋友模板功能的模板类.我目前有以下代码,它正在工作:
template<class T> class Vector { public: template<class U,class W> friend Vector<U> operator*(const W lhs,const Vector<U>& rhs); } template<class U,class W> Vector<U> operator*(const W lhs,const Vector<U>& rhs) { // Multiplication } 我希望我的解决方案能够向朋友函数进行前向声明,这样我就可以获得安全优势,并且与我当前的方法相比,它提供了一对一的通信.我尝试了以下但仍然遇到错误. template<class T> class Vector; template<class T,class W> Vector<T> operator*(const W lhs,const Vector<T>& rhs); template<class T> class Vector { public: friend Vector<T> (::operator*<>)(const W lhs,const Vector<T>& rhs); } template<class T,const Vector<T>& rhs) { // Multiplication } 解决方法
我想你几乎拥有它.当你交友时,你只需要将函数作为单个参数模板.以下编译在g 4.5上,虽然因为我无法用你的测试用例测试实例化,所以我不能100%肯定它会解决你的真正问题.
template<class T> class Vector; template<class T,const Vector<T>& rhs); template<class T> class Vector { public: template<class W> friend Vector<T> operator*(const W lhs,const Vector<T>& rhs); }; template<class T,const Vector<T>& rhs) { // Multiplication } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |