简体   繁体   English

C ++中的功能模板?

[英]Function templates in c++?

For school, I have an assignment to create a function template and use the users input for dynamic memory. 对于学校,我有一个任务来创建功能模板,并将用户输入用于动态内存。 My code is not working, but my teacher says it is correct and it is just my compiler that does not work. 我的代码不起作用,但是老师说这是正确的,只是我的编译器不起作用。 This is the error I get from the compiler: 这是我从编译器得到的错误:

error: invalid conversion from 'int' to 'int*' [-fpermissive] key = array[I]; 错误:从'int'到'int *'的无效转换[-fpermissive]键= array [I];

I use the compiler MinGW64. 我使用的是MinGW64编译器。 Is there anyone with the same problem who has a solution? 有没有同样问题的人有解决方案?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

template <class S>  
void sortArray(S &array, int n);

template <class R>
void reverseArray(R &array, int n);

template <class P>
void printArray(P array, int n);

int main() {
    int n1, n2, i;

    srand(time(0));

    cout << "insert how many numbers:";
    cin >> n1;
    int *nums = new int[n1];
    if(nums == 0){
        cout << "No memory allocated";
        exit(1);
    }
    for(i = 0; i < n1; i++){
        nums[i] = rand() % 100 + 1;
    }
    printArray(nums, n1);
    sortArray(nums, n1);
    printArray(nums, n1);
    reverseArray(nums, n1);
    printArray(nums, n1);
    delete []nums;

    cout << "insert how many characters:";
    cin >> n2;
    char *nums2 = new char[n2];
    if(nums2 == 0){
        cout << "No memory allocated";
        exit(1);
    }
    for(i = 0; i < n2; i++){
        nums2[i] =  rand() % 25 + 97; // rand() % 93 + 33
    }
    printArray(nums2, n2);
    sortArray(nums2, n2);
    printArray(nums2, n2);
    reverseArray(nums2, n2);
    printArray(nums2, n2);
    delete []nums2;

    return 0;
}

template <class S>
void sortArray(S &array, int n){
    int i, j;
    S key;
    for(i = 1; i < n; i++){
        key = array[i];
        for(j = i-1; (j>=0) && (array[j] > key); j--){
             array[j+1] = array[j];
        }
        array[j+1] = key;
    }
}

template <class R>
void reverseArray(R &array, int n){
    int start = 0, end = n-1;
    R temp;
    while(start < end){
        temp = array[start];
        array[start] = array[end];
        array[end] = temp;
        start++;
        end--;
    }
}

template <class P>
void printArray(P array, int n){
    int i;
    for (i = 0; i < n; ++i) {
         cout << "[" <<i << "] => " << array[i] << ", ";
    }
    cout << endl;
}

The compiler is definitely not wrong. 编译器绝对没有错。

There is some miscommunication. 沟通有误。

When you declare 当你声明

template <class S>  
void sortArray(S &array, int n);

and ust it as 并把它当作

sortArray(nums, n1);

S is deduced as int* given the definition of nums in your posted code. 给定发布代码中nums的定义, S推导为int* Hence, you may not use 因此,您可能无法使用

S key;
for(i = 1; i < n; i++){
    key = array[i];

since key is of type int* while array[i] is of type int . 因为keyint*类型,而array[i]int类型。

You can fix the problem by using one of the following two options. 您可以使用以下两个选项之一解决此问题。

  1. Change the function declaration to 将函数声明更改为

     template <class S> void sortArray(S* array, int n); 
  2. Change the type of key . 更改key的类型。

     typename std::remove_reference<decltype(*array)>::type key; 

    or let the compiler deduce it using auto . 或者让编译器使用auto推断它。

     auto key = array[0]; 

    The downside of using the second method is that the program will have undefined behavior if array is empty. 使用第二种方法的缺点是,如果array为空,则程序将具有未定义的行为。

You'll have to change reverseArray similarly. 您必须类似地更改reverseArray

Thank you for all the information. 感谢您提供的所有信息。 My teacher wouldn't help me because he was convinced it was my compiler. 我的老师不会帮助我,因为他坚信这是我的编译器。
So all I had to do was chancing my function declaration to void sortArray(S *array, int n); 所以我要做的就是将函数声明void sortArray(S *array, int n); and now it works perfectly. 现在它可以完美运行了。

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

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