C++ 'No matching function for call to' 模板

C++ 'No matching function for call to' template

我有一个家庭作业,我需要修改旧作业以使用模板。

最初的作业要求对动态分配的整数数组进行随机排序,我需要创建一个模板将其扩展到字符串和双精度数。我正在使用一个 while 循环和一个 switch-case 来向用户呈现一个菜单,他们可以在其中决定他们使用的是哪种类型并填充数组。

我试图尽可能减少它,以使调试更容易。整数实现不会出现编译器错误,但双精度和字符串变体会出现。

int* pPresortedInts = NULL; //pointer to array of ints
std::cout << std::endl << "How many integers did you want to sort? ";
std::cin >> sizeOfArray;
pPresortedInts = new int[sizeOfArray]; //dynamically allocate array
                
//prompt user to fill array
std::cout << std::endl << "Great! Please enter " << sizeOfArray << " numbers: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
    std::cin >> pPresortedInts[counter];
}
                
//output filled array
std::cout << std::endl << std::endl << "Here are the integers in their original order: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
    std::cout << std::endl << pPresortedInts[counter];
}
std::cout << std::endl << std::endl;
                
//shuffle elements and print the result
shuffleSort(pPresortedInts, sizeOfArray);
                
//Remember to delete dynamically allocated arrays
delete [] pPresortedInts;
pPresortedInts = NULL;

和双重实现:

double* pPresortedDoubles = NULL; //pointer to array of doubles
std::cout << std::endl << "How many doubles did you want to sort? ";
std::cin >> sizeOfArray;
pPresortedDoubles = new double[sizeOfArray]; //dynamically allocate array
                
//prompt user to fill array
std::cout << std::endl << "Great! Please enter " << sizeOfArray << " numbers: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
    std::cin >> pPresortedDoubles[counter];
}
                
//output filled array
std::cout << std::endl << std::endl << "Here are the doubles in their original order: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
    std::cout << std::endl << pPresortedDoubles[counter];
}
std::cout << std::endl << std::endl;
                
//shuffle elements and print the result
shuffleSort(pPresortedDoubles, sizeOfArray);
                
//Remember to delete dynamically allocated arrays
delete [] pPresortedDoubles;
pPresortedDoubles = NULL;

最后,这是模板本身:

template <class T>
void shuffleSort(T pPresortedArray[], T size) {
    T* pShuffledArray = new T[size]; // pointer to dynamically allocated array of a generic type
    T randomElement;
    T temp;
    
    //fill ShuffledArray with PresortedArray
    for (int counter = 0; counter < size; counter++) {
        pShuffledArray[counter] = pPresortedArray[counter];
    }
    
    for (int counter = 0; counter < size; counter++) {
        randomElement = rand()%size; // choose a random element from the ShuffledArray array
        //swap that element with the currently selected counter
        temp = pShuffledArray[randomElement];
        pShuffledArray[randomElement] = pShuffledArray[counter];
        pShuffledArray[counter] = temp;
    }
    std::cout << "Shuffled array: ";
    //print the (hopefully) shuffled array
    for (int counter = 0; counter < size; counter++) {
        std::cout << std::endl << pShuffledArray[counter];
    }
    std::cout << std::endl << std::endl;
    
    //Delete dynamically allocated array within scope
    delete [] pShuffledArray;
    pShuffledArray = NULL;
}

我也试过 specifying the type at time of the function call, but that didn't fix the error. I don't believe that my issue has anything to do with taking arguments by reference

我倾向于更喜欢 Gist 来阅读代码,所以我上传了 entire program there,连同作业描述(以防有帮助)。

感谢您的帮助!

好吧,这似乎是错误的

void shuffleSort(T pPresortedArray[], T size) {

为什么要模板化数组的大小,它总是 size_t

void shuffleSort(T pPresortedArray[], size_t size) {

我怀疑您将 'int' 全局替换为 'T' :-)

数组的大小应该是 size_tint,但无论如何都不是 T :)

除此之外,这是一段相当不错的代码。

您可以使用 std::swap 稍微简化一下。

此外,您输入的代码也可以很容易地制成模板,以避免大量重复。

在这一行:

void shuffleSort(T pPresortedArray[], T size)

您真的希望参数大小的类型是 T 吗?

并且您可以使用 std::random_shuffle random_shuffle API 和一个新的 API

std::shuffle API