无法推断模板参数/没有' n'重载可以转换所有参数类型

时间:2015-09-16 03:24:50

标签: c++ templates

我正在尝试通过Jan Bartipan编译一个名为vmath的矢量库。

有一些函数可以扩展vector的std命名空间,如下所示:

#define VEC3 Vector3

namespace std
{
    //...

    template <typename T>
    VEC3<T> min(const VEC3<T>& a, const VEC3<T>& b)
    {
        return VEC3<T>(::std::min(a.x, b.x), ::std::min(a.y, b.y), ::std::min(a.z, b.z));
    }

    //...
}

xyz是Vector3的成员,类型为T

当我尝试编译此代码时,出现以下错误:

error C2784: 'Vector3<T> std::min(const Vector3<T> &,const Vector3<T> &)' : could not deduce template argument for 'const Vector3<T> &' from 'const double'

我环顾四周,发现可能需要模仿对std::min的调用。所以我尝试将代码更改为以下内容:

template <typename T>
VEC3<T> min(const VEC3<T>& a, const VEC3<T>& b)
{
    return VEC3<T>(::std::min<T>(a.x, b.x), ::std::min<T>(a.y, b.y), ::std::min<T>(a.z, b.z));
}

但是当我尝试编译它时,我收到以下错误:

error C2665: 'std::min' : none of the 3 overloads could convert all the argument types

我希望你们都能清楚地了解我做错了什么。

谢谢!

编辑:我正在使用Visual Studio 2013进行编译

1 个答案:

答案 0 :(得分:2)

由于您未在<algorithm>声明std::min,因此您对std::min(const VEC3<T>&, const VEC3<T>&)的定义只会看到自己。

因此std::min(a.x, b.x)尝试匹配唯一可能的重载std::min(const VEC3<T>&, const VEC3<T>&),而不能a.xdouble

#include <algorithm>可以解决您的问题。