visual-c++ - 合并排序C++无效[歧义错误]

标签 visual-c++ compiler-errors mergesort

我有一个合并排序,当我这样做时它将起作用

mergeSort<int>(val_array1,numValues);

但是一旦我将其更改为 float
mergeSort<float>(val_array2,numValues);

我收到此错误:
1>c:\users\cbadau\documents\visual studio 2010\projects\lab\lab\lab12.cpp(71): error C2782: 'void recMergeSort(ItemType [],ItemType,ItemType)' : template parameter 'ItemType' is ambiguous
1>          c:\users\cbadau\documents\visual studio 2010\projects\lab\lab\lab12.cpp(58) : see declaration of 'recMergeSort'
1>          could be 'int'
1>          or       'float'
1>          c:\users\cbadau\documents\visual studio 2010\projects\lab\lab\lab12.cpp(96) : see reference to function template instantiation 'void mergeSort<float>(ItemType [],int)' being compiled
1>          with
1>          [
1>              ItemType=float
1>          ]
1>

源代码:
#include<iostream>
using namespace std;

template<class ItemType>
void merge(ItemType list[], ItemType first, ItemType last, ItemType mid) 
{
    ItemType arraySize = last - first + 1;
    ItemType* tempList = new ItemType[arraySize];
    ItemType beginPart1 = first;
    ItemType endPart1 = mid;
    ItemType beginPart2 = mid + 1;
    ItemType endPart2 = last;

    int index = 0;


    while (beginPart1 <= endPart1 && beginPart2 <= endPart2) {
        if (list[beginPart1] < list[beginPart2]) {
            tempList[index] = list[beginPart1];
            beginPart1++;
        }
        else {
            tempList[index] = list[beginPart2];
            beginPart2++;
        }
        index++;
    }

    while (beginPart1 <= endPart1) {
        tempList[index] = list[beginPart1];
        index++;
        beginPart1++;
    }

    while (beginPart2 <= endPart2) {
        tempList[index] = list[beginPart2];
        index++;
        beginPart2++;
    }


    for (int i = first; i <= last; i++) {
        list[i] = tempList[i - first];
    }

    delete[] tempList;
}

template<class ItemType>
void recMergeSort(ItemType list[], ItemType first, ItemType last) 
{
    if (first < last) {
        ItemType mid = (first + last) / 2;
        recMergeSort(list, first, mid);
        recMergeSort(list, mid + 1, last);
        merge(list, first, last, mid);
    }
}

template<class ItemType>
void mergeSort(ItemType list[], int length) 
{
    recMergeSort(list, 0, length - 1);
}



int main()
{

int  val_array1[] = {43, 7, 10, 23, 38, 4, 19, 51, 66, 14};
float  val_array2[] = {43.2, 7.1, 10.5, 3.9, 18.7, 4.2, 19.3, 5.7, 66.8, 14.4};
int numValues = 10;

cout<<"val_array1 unsorted: "<<endl;
for(int i = 0; i <numValues; i++)
{
    cout<<val_array1[i]<<endl;
}

cout<<"val_array2 unsorted: "<<endl;
for(int x=0; x <numValues; x++)
{
cout<<val_array2[x]<<endl;
}

mergeSort<int>(val_array1,numValues);
mergeSort<float>(val_array2,numValues);

cout<<"val_array1 sorted: "<<endl;
for(int y = 0; y <numValues; y++)
{
    cout<<val_array1[y]<<endl;
}

cout<<"val_array2 sorted: "<<endl;
for(int t=0; t <numValues; t++)
{
cout<<val_array2[t]<<endl;
}




system("pause");
return 0;
}

关于问题是什么?

最佳答案

值的类型与模板中的索引的类型相同。将firstlastmid等的ItemType更改为int。

关于visual-c++ - 合并排序C++无效[歧义错误],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8527540/

相关文章:

c++ - 如何将退格键更改为 '\b' ?

macos - ld : symbol(s) not found when linking

vba - WorksheetFunction 错误子或函数未定义

c++ - 将实例传递给另一个类并访问它 C++

c++ - "try catch(...)"可以在 C++ 中捕获哪些异常?

c++ - 在 Windows 中更改原始波形数据的音高

c++ - 错误 C2361 : initialization of 'found' is skipped by 'default' label

algorithm - 剖析合并排序例程

c - Mergesort - 段错误

java - 理解Java合并排序的排序部分