Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:无法将int**转换为int*(模板、动态分配数组)_C++_Oop - Fatal编程技术网

C++ 错误:无法将int**转换为int*(模板、动态分配数组)

C++ 错误:无法将int**转换为int*(模板、动态分配数组),c++,oop,C++,Oop,我已经编写了3个模板函数,但是当我运行代码时,它在第一个函数体上给出了错误,其中动态分配了arr的内存。下面是代码,请帮我找到我错过的东西。谢谢 错误:错误1错误C2440:“=”:无法从“int**”转换为“int*” #include<iostream> using namespace std; template<typename T> void input(T arr, int size){ arr = new T[size]; for(int

我已经编写了3个模板函数,但是当我运行代码时,它在第一个函数体上给出了错误,其中动态分配了arr的内存。下面是代码,请帮我找到我错过的东西。谢谢

错误:错误1错误C2440:“=”:无法从“int**”转换为“int*”

#include<iostream>
using namespace std;

template<typename T>
void input(T arr, int size){
    arr = new T[size];

    for(int i=0; i<size; i++){
        cout<<"\nEnter: ";
        cin>>arr[i];    
    }
}


template<typename T>
void sort(T arr, int size){
    int temp;

    for(int j=0; j<size-1; j++){
        for(int i=0; i<size-1; i++){
            if(arr[i]>arr[i+1]){
                    temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
            }
        }
    }
}


template<typename T>
void display(T arr, int size){

    cout<<"\nAfter Sorting: "<<endl;

    for(int i=0; i<size; i++){
        cout<<arr[i]<<"\t";
    }
}

int main(){
    int* x=NULL;
    int size;

    cout<<"Enter the number of elements: ";
    cin>>size;

    cout<<"\nEnter integer values:";
    input<int*>(x, size);
//  sort(x, size);
    display<int*>(x, size);

    /***
    cout<<"\nEnter floating values:";
    input(x, size);
    sort(x, size);
    display(x, size);

    cout<<"\nEnter character values:";
    input(x, size);
    sort(x, size);
    display(x, size);
    */

    system("pause");
}
#包括
使用名称空间std;
模板
无效输入(T arr,int size){
arr=新T[尺寸];
对于(int i=0;i您在这里有一个bug:

arr = new T[size];
对于您的参数,它意味着:

int *arr = new int*[size];
template<typename T>
void input(T * &arr, int size){//
    arr = new T[size];

    for(int i=0; i<size; i++){
        cout<<"\nEnter: ";
        cin>>arr[i];    
    }
}
newint*[size]
的类型不是
int*

您可以执行以下操作:

int *arr = new int*[size];
template<typename T>
void input(T * &arr, int size){//
    arr = new T[size];

    for(int i=0; i<size; i++){
        cout<<"\nEnter: ";
        cin>>arr[i];    
    }
}
模板
无效输入(T*&arr,整数大小){//
arr=新T[尺寸];

对于(int i=0;i请不要尝试在
中添加格式。先生,我不明白您所说的是哪种格式?
**arr=new t[size];***
这实际上是什么?哦,好的。谢谢我更正了它。代码中还有一个错误:
输入(x,size)
太好了。问题解决了。但当我使用float type by etc时,它运行正常,但输出结果时没有小数,就像它将2.1作为2一样。@ZeshanMuztar您是否仍在main中使用x(int*)作为输入?如果您的答案是肯定的,那么您就错了,您必须在main中使用float*,而不是int*