C++ 模板问题

C++ 模板问题,c++,templates,C++,Templates,我有一个简单的排序程序,它应该适用于Set-s的向量,它适用于基本类型,我对非基本类型使用了一些其他比较函数,它工作得很好,但一旦我尝试比较集合,它就会出错: 错误C2782:'void Sort(Vector&,int(uu cdecl*)(type,type))':模板参数'type'不明确 我怎样才能解决这个问题 template <typename type> void swap(int &a, int &b){ type tmp =a; a

我有一个简单的排序程序,它应该适用于
Set
-s的向量,它适用于基本类型,我对非基本类型使用了一些其他比较函数,它工作得很好,但一旦我尝试比较集合,它就会出错:

错误C2782:'void Sort(Vector&,int(uu cdecl*)(type,type))':模板参数'type'不明确

我怎样才能解决这个问题

template <typename type>
void swap(int &a, int &b){
    type tmp =a;
    a = b;
    b = tmp;
}


template <typename type>
void Sort(Vector<type>& vec,int (cmp) (type,type) = OperatorCmp ){
    while(true){
        for(int i =1; i < v.size(); i++){
        if(cmp(v[i-1],v[i]) > 0){
           break;
            }else{
            return;
         }
    }

        int index1 = RandomInteger(0,vec.size()-1);
        int index2 = RandomInteger(0,vec.size()-1);
        swap(vec[index1],vec[index2]);
    }
}


int main(){
    Randomize();
    Vector<char>a;

    Sort(a);


    return 0;
}
模板
无效掉期(内部和a、内部和b){
tmp型=a型;
a=b;
b=tmp;
}
样板
无效排序(向量和向量,整数(cmp)(类型,类型)=运算符cmp){
while(true){
对于(int i=1;i0){
打破
}否则{
回来
}
}
int index1=RandomInteger(0,向量大小()-1);
int index2=RandomInteger(0,向量大小()-1);
互换(向量向量[index1],向量[index2]);
}
}
int main(){
随机化();
向量;
排序(a);
返回0;
}
您有

template <typename type>
void bozoSort(Vector<type>& vec,int (cmp) (type,type) = OperatorCmp )
那是

int compareSets(Set<int>& a , Set<int>& b){
int比较集(集合a、集合b){
现在对于
vec
type
只能
Set
,而对于
cmp
type
只能
Set&
。两者不能同时进行,因此会出现错误。

template <typename type>
void bozoSort(Vector<type>& vec,int (cmp) (type,type) = OperatorCmp )
那是

int compareSets(Set<int>& a , Set<int>& b){
int比较集(集合a、集合b){

现在对于
vec
type
只能是
Set
,而对于
cmp
type
只能是
Set&
。两者不能同时进行,因此会产生错误。

type
被推断为函数指针参数的
Set
,以及co值类型的
Set
ntainer,这是一个不一致的推论。最简单的解决方案:完全推广函子:

template <typename type, typename Fun>
bool isSorted(Vector<type> & vec, Fun cmp){
    for(int i =0; i < vec.size()-1; i++){
        if(cmp(vec[i],vec[i+1]) > 0)return false;
    }
    return true;
}
模板
bool isSorted(矢量和矢量,趣味cmp){
对于(int i=0;i0,则返回false;
}
返回true;
}

..和重载用于单参数情况。

类型
被推导为函数指针参数的
,容器值类型的
,这是一个不一致的推导。最简单的解决方案:完全概括函子:

template <typename type, typename Fun>
bool isSorted(Vector<type> & vec, Fun cmp){
    for(int i =0; i < vec.size()-1; i++){
        if(cmp(vec[i],vec[i+1]) > 0)return false;
    }
    return true;
}
模板
bool isSorted(矢量和矢量,趣味cmp){
对于(int i=0;i0,则返回false;
}
返回true;
}

…和一个参数情况下的重载。

您的类型不匹配。
bozoSort
声明为:

template <typename T>
void bozoSort(Vector<T>& vec, int (cmp) (T,T) );
但是,您使用
compareSets
调用它,它具有签名
int(Set&,Set&)
。这些签名不匹配,因此编译器无法为您解析模板。更好的解决方案是将整个比较器作为模板:

template <typename T, typename Compare>
void bozoSort(Vector<T>& vec, Compare cmp) { ... }
模板
void bozoSort(Vector&vec,Compare cmp){…}

这样,如果您希望比较器通过引用、常量引用或值获取其参数,则上述任何一项都可以正常工作。

您存在类型不匹配。
bozoSort
声明为:

template <typename T>
void bozoSort(Vector<T>& vec, int (cmp) (T,T) );
但是,您使用
compareSets
调用它,它具有签名
int(Set&,Set&)
。这些签名不匹配,因此编译器无法为您解析模板。更好的解决方案是将整个比较器作为模板:

template <typename T, typename Compare>
void bozoSort(Vector<T>& vec, Compare cmp) { ... }
模板
void bozoSort(Vector&vec,Compare cmp){…}

这样,如果您希望比较器通过引用、常量引用或值获取其参数,则上述任何一种方法都可以。我认为演绎法在这里不起作用。这远远不是一个最小的完整示例。@LightnessRacesinOrbit是的。它只会产生不一致的演绎。我认为演绎法在这里不起作用。这是远远不是一个最小的、完整的例子。@LightnessRacesinOrbit确实如此。它只会产生不一致的扣减。