c++ - 没有匹配的函数调用未解析的函数类型

标签 c++ templates stl c++11

这段代码有问题,它说没有多次匹配的函数调用,尽管我使用相同的数据类型并且参数的数量相同,有人可以帮我吗?

#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include<list>
#include<deque>
#include <algorithm>
#include <chrono>
#include <functional>
#include <sstream>

using namespace std;
using namespace std::chrono;


template<typename F, typename Arg, typename T>
void times(F func, Arg A, int n, T typeval)    // call func(arg,n, typeval)
{
    auto t1 = system_clock::now();
    func(A, n, typval);
    auto t2 = system_clock::now();
    auto dms = duration_cast<milliseconds>(t2-t1);
    cout << "f(x) took " << dms.count() << " milliseconds\n";
}



int random_gen(int& i){
    default_random_engine re { std::random_device()() };
    uniform_int_distribution<int> dist;
    auto r= bind(dist,re);
    int x =r();
    return x;
    //return rand();
}

string random_gen(string& s)
{
    string Result;          // string which will contain the result
    ostringstream convert;   // stream used for the conversion
    convert << rand();
    return convert.str();
}

template<typename SequenceContainer, typename T>
void build_cont(SequenceContainer& seq, int n, T valtype)
{
    for(int i=0; i!=n; ++i) {
        T gen = random_gen(valtype);
        typename SequenceContainer::iterator it = find_if(seq.begin(), seq.end(), [gen] (const typename SequenceContainer::reference & val) { return gen < val; } );
        seq.insert(it, gen);
    }
    for(auto i:seq)
        cout<<i<<endl;
}
int main() {
    int n=10;
    vector<int> v;
    list<int>ls;
    deque<int> deq;
   cout<<"vector of int"<<endl;
    times(build_cont, v, n, 0);

//  string stemp = "";
//    cout<<"vector of strings"<<endl;
//    build_cont(sv, n, stemp);
//    cout<<"list of strings"<<endl;
//    build_cont(sls, n, stemp);
//    cout<<"deque of strings"<<endl;
//    build_cont(sdeq, n, stemp);
//    
    return 0;
}

当我运行 build_cont 时它运行得很好,但是当我把它放在 times 函数中时代码停止工作了? 任何建议。

最佳答案

需要为build_cont指定模板参数

times(build_cont<std::vector<int>, int>, v, n, 0);

关于c++ - 没有匹配的函数调用未解析的函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084943/

相关文章:

c++ - 创建采用可变数量的参数和数据类型的函数

c++ - 用于默认模板参数的参数包

c++ - 重命名 map 迭代器的第一个和第二个

c++ - 为什么 std::sort 假定 std::vector< std::vector<int>> 默认为 std::vector,从而产生错误的结果?

c++ - 如何在 Magick++ 中为文本添加自动换行

c++ - C++中不带virtual的多态实现多级继承

c++ - 在派生类模板中专门化基类的成员函数

c++ - 我如何知道 C++ 模板是容器还是类型?

C++:是否可以从重载 << 运算符的对象中获取 std::string?

c++ - 引用计数(不存储任何数据)