我写了几行:
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
template <class T> struct First
{
T num;
First() {}
First(const T &a) : num(a) {}
};
template <typename var> bool criterio(First<var> &primo, First<var> &secondo)
{
return (primo.num < secondo.num);
}
int main()
{
vector< First<int> > f;
srand (time(NULL));
for(int i=0; i<20; i++) f.push_back( First<int>(rand() % 20) );
sort(f.begin(),f.end(),criterio);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用"g ++ program2.C"编译,答案是:
program2.C:在函数'int main()'中:
program2.C:28:错误:没有匹配的函数调用toort(__ gnu_cxx :: __ normal_iterator*,std :: vector,std :: allocator >>>,__ gn_cxx :: __ normal_iterator*,std :: vector,std :: allocator >>>,unresolved重载函数类型)'
我不知道它是什么类型的问题......你能帮助我吗?
感谢帮助...
小智 7
criterio是一个模板,所以你需要给出模板化的类型:
sort(f.begin(),f.end(),criterio<int>)
Run Code Online (Sandbox Code Playgroud)
而且标准函数必须将const引用作为参数:
template <typename var> bool criterio(const First<var> &primo,
const First<var> &secondo)
{
return (primo.num < secondo.num);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2155 次 |
| 最近记录: |