简体   繁体   English

C ++“无匹配函数可调用”模板

[英]C++ 'No matching function for call to' template

I have a homework assignment where I need to modify an older assignment to use templates. 我有一个家庭作业,需要修改较早的作业以使用模板。

The original assignment called for random sorting of a dynamically allocated array of integers and I need to create a template that extends this to strings and doubles. 原始分配要求对动态分配的整数数组进行随机排序,我需要创建一个模板,将其扩展为字符串和双精度。 I'm using a while loop and a switch-case to present a menu to the user, where they can decide which type they're using and to fill in the array. 我正在使用while循环和切换用例来向用户展示菜单,他们可以在其中确定所使用的类型并填写数组。

I've tried to cut this down as much as possible, to make debugging easier. 我试图尽可能减少这种情况,以使调试更加容易。 The integer implementation gets no compiler errors, but the double and string variations do. 整数实现没有编译器错误,但是double和string变体可以。

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;

And the double implementation: 和双重实现:

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;

Finally, here's the template itself: 最后,这是模板本身:

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;
}

I've tried 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 , either. 我也不认为我的问题与通过引用进行论证有任何关系。

I tend to prefer Gist for reading code, so I've uploaded the entire program there , along with the assignment description (in case that's helpful somehow). 我倾向于使用Gist来阅读代码,因此,我已将整个程序以及作业说明(如果有帮助的话)上传到了那里

Thanks for any help! 谢谢你的帮助!

well this seems wrong 好吧,这似乎是错误的

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

WHy would you template the size of the array, its always going to be size_t 为什么要模板化数组的大小,其大小总是为size_t

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

I suspect you did a global replace of 'int' by 'T' :-) 我怀疑您用'T'代替了'int'的全局位置:-)

The size of your array should be a size_t or an int , but not a T at any rate :) 数组的大小应为size_tint ,但无论如何不应为T :)

Except for that, it's a pretty good piece of code. 除此之外,这是一段相当不错的代码。

You could use std::swap to simplify it a bit. 您可以使用std::swap进行简化。

Also, your input code could easily be made into a template too, to avoid heavy duplication. 而且,您的输入代码也可以轻松地制成模板,以避免繁琐的重复。

At this line: 在这一行:

void shuffleSort(T pPresortedArray[], T size)

Do you really want that argument size's type is T. 您是否真的希望参数大小的类型为T。

And you can use std::random_shuffle random_shuffle API and a new API 您可以使用std :: random_shuffle random_shuffle API和新的API

std::shuffle API std :: shuffle API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM