无法使用类型 'int (*)[20]' 的右值初始化 'int *' 类型的参数

  • 本文关键字:int 类型 初始化 参数 c++
  • 更新时间 :
  • 英文 :


Main.cpp

#include "SelectionSort.h"
using namespace std;

int main() {
    SelectionSort<int> sorterInt;
    int test_array[20];
    sorterInt.stuffNum(&test_array, 20, 1, 200);
}

SelectionSort.h

using namespace std;
template <typename T>
class SelectionSort {
public:
    void stuffNum(T *object, int size, int min, int max)
    {
        for(int i = 0; i < size; i++)
        {

            (*object)[i] = 5;

        }
    }
SelectionSort<int> sorterInt;
int test_array[20];
sorterInt.stuffNum(&test_array, 20, 1, 200);

您的模板具有int类型,因此您的方法采用int*作为参数。你写了&test_array类型为int*[20]的用户,因为您发送了数组的地址

所以只需删除&

sorterInt.stuffNum(test_array, 20, 1, 200);

您需要更好地理解指针。

编辑:(阅读评论(

(*object)[i] = 5;

在这里你应该像这个一样删除*和((

object[i] = 5;

这里有更多的医生什么是阵法腐朽?

最新更新