无法将我的功能转换为模板形式

时间:2017-04-18 05:39:36

标签: c++ templates

我一直在练习C ++并且使用不同的编码样本。我已经能够列出所有整数排列,但我想将它扩展到任何项目的任何向量。我尝试使用模板,实际上我所做的只是定义模板并将int替换为T ,如下所示,但我必须以某种方式弄乱语法,因为我我在构建时遇到错误。

据我了解,当我传递模板函数时,参数的类型定义了模板的类型,有效地用我推断的类型替换Ttemplate <typename T>类型的所有实例。因此,在传递add_combinations_to(vector<int>, vector<vector<int>>::iterator, vector<vector<int>>::iterator, vector<vector<int>>)时,我将T替换为int,但编译器无法理解。

template <typename T>
void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
    if(start == stop) {ret.push_back(prefix); return;}
    for(auto v : *start) {
        auto next = prefix;
        next.push_back(v);
        add_combinations_to(next, start + 1, stop, ret);
    }
};

int main() {
    vector<vector<int>> v{{1,2},{3,4}};
    vector<vector<int>> ret;
    vector<int> init{};
    add_combinations_to(init, v.begin(),v.end(),ret);
    // ret should be: {{1, 3},{1, 4},{2, 3},{2, 4}}
}

错误:

C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:63: error: 'std::vector<std::vector<_RealType, std::allocator<_CharT> >, std::allocator<std::vector<_RealType, std::allocator<_CharT> > > >::iterator' is not a type
 void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
                                                               ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:98: error: 'std::vector<std::vector<_RealType, std::allocator<_CharT> >, std::allocator<std::vector<_RealType, std::allocator<_CharT> > > >::iterator' is not a type
 void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
                                                                                                  ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp: In function 'void add_combinations_to(std::vector<_RealType>, int, int, std::vector<std::vector<_RealType> >&)':
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:67:19: error: invalid type argument of unary '*' (have 'int')
     for(auto v : *start) {
                   ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp: In function 'int main()':
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:79:52: error: no matching function for call to 'add_combinations_to(std::vector<int>&, std::vector<std::vector<int> >::iterator, std::vector<std::vector<int> >::iterator, std::vector<std::vector<int> >&)'
     add_combinations_to(init, v.begin(),v.end(),ret);
                                                    ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:6: note: candidate: template<class T> void add_combinations_to(std::vector<_RealType>, int, int, std::vector<std::vector<_RealType> >&)
 void add_combinations_to(vector<T> prefix, vector<vector<T>>::iterator start, vector<vector<T>>::iterator stop, vector<vector<T>>& ret) {
      ^
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:65:6: note:   template argument deduction/substitution failed:
C:\Users\Peter\CLionProjects\CodingPractice\main.cpp:79:38: note:   cannot convert 'v.std::vector<_Tp, _Alloc>::begin<std::vector<int>, std::allocator<std::vector<int> > >()' (type 'std::vector<std::vector<int> >::iterator {aka __gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >}') to type 'int'
     add_combinations_to(init, v.begin(),v.end(),ret);

任何人都可以提供帮助,说明为什么会发生这种情况,以及如何在一般情况下使其发挥作用?

0 个答案:

没有答案