向量传染媒介的总和...整数

时间:2017-11-07 18:41:50

标签: c++ templates vector

我试图总结向量矢量向量的所有元素...整数:类似于std::vector<std::vector<std::vector<std::vector<int>>>>,其中每个层都不需要具有相同的大小

我想用模板完成它,所以我做到了:

namespace nn
{
    template < class T >
    int sumAllElements(std::vector<T> v)
    {
        int size = v.size();
        int output = 0;

        for ( int i = 0 ; i < size ; i++ )
        {
                                                //should call the function below 
            output += sumAllElements( v[ i ] ); //or this function, depending in 
                                                //which "layer" we are
        }

        return output;
    }

    int sumAllElements(std::vector<int> v)
    {
        int size = v.size();
        int output = 0;

        for ( int i = 0 ; i < size ; i++ )
        {
            output += v[ i ]; //we've reached the bottomest layer,
                              //so just sum everybory
        }

        return output;
    }
}

但是,这种情况正在发生:

CMakeFiles\test.dir/objects.a(main.cpp.obj): In function `main':
D:/test/main.cpp:49: undefined reference to `int nn::sumAllElements<std::vector<int, std::allocator<int> > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\test\build.make:141: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: test] Error 2

我真的不知道为什么......

提前致谢。

3 个答案:

答案 0 :(得分:2)

您无法调用尚未声明的函数。模板有时会使问题消失,但并非总是如此。这是您在int sumAllElements(std::vector<int> v)

之前需要声明template < class T > int sumAllElements(std::vector<T> v)的情况之一

答案 1 :(得分:2)

阅读您的错误讯息。看起来你的函数与main.cpp在一个单独的编译单元中。如果你的函数在.h文件中,#include是main.cpp中的头文件。

我建议使用模板专业化声明:

template<>
int sumAllElements(std::vector<int> v)
{
 ...   
}

另一个不相关的建议是通过const引用传递向量。目前,您通过值传递它们,如果向量很大,这可能会很昂贵。

答案 2 :(得分:1)

您可以使用SFINAE启用/禁用所需的专业化:

template <class T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
auto sum_all(const std::vector<T>& v)
{
    T sum = 0;

    for (auto& e : v)
    {
        sum += e;
    }

    return sum;
}

template <class T, std::enable_if_t<!std::is_arithmetic<T>::value, int> = 0>
auto sum_all(const std::vector<T>& nested_v)
{
    decltype(sum_all(nested_v[0])) sum = 0;

    for (auto& e : nested_v)
    {
        sum += sum_all(e);
    }

    return sum;
}

on coliru

使用C ++ 17,您只能拥有一个功能(整洁!):

template <class T>
auto sum_all(const std::vector<T>& nested_v)
{
    innermost_type_t<T> sum = 0;

    for (auto& e : nested_v)
    {
        if constexpr(std::is_arithmetic<T>::value)
            sum += e;
        else
            sum += sum_all(e);
    }

    return sum;
}

innermost_type_t定义为:

template <class T> struct innermost_type
{
    using type = T;
};

template <class T> struct innermost_type<std::vector<T>>
{
    using type = typename innermost_type<T>::type;
};

template <class T>
using innermost_type_t = typename innermost_type<T>::type;
相关问题