简体   繁体   English

错误:没有匹配的函数来调用[…]注意:模板参数推导/替换失败

[英]error: no matching function for call to […] note: template argument deduction/substitution failed

template <int dim = 2>
class point {
private:
    std::array<double, dim> coords;
public:
    /* Constructors, destructor, some members [...] */
    template <typename operation_aux, int dim_aux> point<dim_aux>
        plus_minus_helper(const point<dim_aux>& op1, const point<dim_aux>& op2);
};

template <typename operation, int dim> point<dim>
    plus_minus_helper(const point<dim>& op1, const point<dim>& op2)
{
    /* Do all the calculations.
     * The algorithm is very similar for both addition and subtraction,
     * so I'd like to write it once
     */
}

template <int dim>
    point<dim> operator+(const point<dim>& op1, const point<dim>& op2)
{
    return plus_minus_helper<std::plus>(op1, op2);
}

template <int dim>
    point<dim> operator-(const point<dim>& op1, const point<dim>& op2)
{
    return plus_minus_helper<std::minus>(op1, op2);
}

int main(int argc, char * argv []) {
    point<2> Pt1(1, 2), Pt2(1, 7), Pt3(0, 0);

    Pt3 = Pt1 + Pt2;
    std::cout << Pt3(0) << " " << Pt3(1) << std::endl;
}

Compiling this code on GCC produces the following error 在GCC上编译此代码会产生以下错误

./test.cpp: In instantiation of ‘point<dim> operator+(const point<dim>&, const point<dim>&) [with int dim = 2]’:
./test.cpp:47:17:   required from here
./test.cpp:35:49: error: no matching function for call to ‘plus_minus_helper(const point<2>&, const point<2>&)’
     return plus_minus_helper<std::plus>(op1, op2);
                                                 ^
./test.cpp:35:49: note: candidate is:
./test.cpp:26:5: note: template<class operation, int dim> point<dim> plus_minus_helper(const point<dim>&, const point<dim>&)
     plus_minus_helper(const point<dim>& op1, const point<dim>& op2)
     ^
./test.cpp:26:5: note:   template argument deduction/substitution failed:

It seems it is a problem with the instatiaton of the template, in fact the - operator does not produce any error. 看来模板的不确定性是一个问题,实际上-运算符不会产生任何错误。 I provided all the necessary types to instantiate the function, an operation and the dimension of the operators. 我提供了所有必要的类型来实例化函数,操作和运算符的维。 The dimension is implicit in the parameters, and I don't think I passed conflicting parameters. 维度是隐式包含在参数中的,我不认为我传递了冲突的参数。
I'm trying to call the helper function passing to it the calculation to execute (addition or subtraction). 我试图调用传递给它的帮助函数来执行计算(加法或减法)。 I don't want to write the algorithm twice one time for addition and one time for subtraction, because they only differ by for the operation performed. 我不想写两次加法和一次减法的算法,因为它们仅因执行的操作而不同。
How to fix this error? 如何解决这个错误?

Problem is, that std::plus / std::minus are template classes. 问题是std::plus / std::minus是模板类。 You should point, which instantiation of class you want to send, or if you can use C++14 - just <> , if you don't want to point, which instantiation compiler should use you can use template template parameter . 您应该指出要发送的类的实例化,或者如果可以使用C ++ 14-仅<> ,则如果要指出的话,可以使用template template parameter

return plus_minus_helper<std::plus<>>(op1, op2);

Live example 现场例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM