Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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/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
C++ 在模板类中创建模板重载运算符_C++_Templates - Fatal编程技术网

C++ 在模板类中创建模板重载运算符

C++ 在模板类中创建模板重载运算符,c++,templates,C++,Templates,昨天我在这里看到一个问题 它有答案,但它不适合我 所以,就像第一个问题一样 NSize<30, unsigned char, unsigned short> a(101); NSize<25, unsigned char, unsigned short> b(120); NSize<15, unsigned char, unsigned short> c(115); NSize<30> res = (a*b)*(a*c); 有什么建议吗

昨天我在这里看到一个问题

它有答案,但它不适合我

所以,就像第一个问题一样

 NSize<30, unsigned char, unsigned short> a(101);
 NSize<25, unsigned char, unsigned short> b(120);
 NSize<15, unsigned char, unsigned short> c(115);
 NSize<30> res = (a*b)*(a*c);

有什么建议吗

如果没有一个可编译的示例,就很难解决这个问题,但对我来说,问题似乎如下:重载乘法运算符仅为默认模板参数定义。就是代码

template <int lengthA, int lengthB>
NSize<lengthA + lengthB>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b)
{
    return NSize<lengthA + lengthB>(...);
}
并将其作为乘法运算符的结果返回:

template<typename ... Args1, typename ... Args2>
auto operator*(Nsize<Args1 ...> const& nsize1, Nsize<Args2 ...> const& nsize2)
    -> typename common_type<Nsize<Args1 ...>, Nsize<Args2 ...> >::type
{
    //...
}

这是实际代码吗?看起来您正在使用非常量references@PiotrSkotnicki是的,那个里有const Nsize,但当我试图让程序工作时改变了它。请把你们的实际代码贴在一个邮箱里。看起来好像你想让你的操作符*成为一个成员函数,在这种情况下,它应该接受一个参数,这就是你的编译器告诉你的。是的,我肯定错过了那部分:
error: 'NSize<max(aS, bS)> NSize<size, basic_type, long_type, base>::operator*(NSize<aS>&, NSize<bS>&)' must take either zero or one argument
     NSize<max(aS,bS)> operator * (NSize<aS> &a, NSize<bS> &b)

error: no match for 'operator*' (operand types are 'NSize<30, unsigned char, short unsigned int>' and 'NSize<30, unsigned char, short unsigned int>')
     NSize<30, unsigned char, unsigned short> resc = (a*b)*(a*c);
template <int lengthA, int lengthB>
NSize<lengthA + lengthB>  operator * (const NSize<lengthA> &a, const NSize<lengthB> &b)
{
    return NSize<lengthA + lengthB>(...);
}
template <int lengthA, int lengthB>
NSize<lengthA + lengthB, unsigned int, unsigned long long, 256>  operator * 
(const NSize<lengthA, unsigned int, unsigned long long, 256> &a, const NSize<lengthB, unsigned int, unsigned long long, 256> &b)
{
    return NSize<lengthA + lengthB, unsigned int, unsigned long long, 256>(...);
}
NSize<some_integer, unsigned char,       unsigned short>
                  //^^^^^^^^^^^^         ^^^^^^^^^^^^^^
                  //not: unsigned int    not: unsigned long long
template<typename T, typename S>
struct common_type
{
    using type = typename std::common_type<T, S>::type;
};

template<int size1, typename basic_type1 /*, ... */
       , int size2, typename basic_type2 /*, .. */>
struct common_type<NSize<size1, basic_type1 /*, ... */>
                 , NSize<size2, basic_type2 /*, ... */> >
{
    using type = NSize<size1+size2, typename std::common_type<basic_type1,basic_type2>::type /*, ... */ >;
};
template<typename ... Args1, typename ... Args2>
auto operator*(Nsize<Args1 ...> const& nsize1, Nsize<Args2 ...> const& nsize2)
    -> typename common_type<Nsize<Args1 ...>, Nsize<Args2 ...> >::type
{
    //...
}