c++ - Error: cannot convert int ** to int * (templates, dynamic allocation arrays) -


i have written 3 template functions when run code, gives error on first function's body memory arr dynamically allocated. following code, please me in finding missed. thanks

error: error 1 error c2440: '=' : cannot convert '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 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"); } 

you have bug here:

arr = new t[size]; 

with parameters, means like:

int *arr = new int*[size]; 

the type of new int*[size] not int*

you can like:

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

and in main:

    input<int>(x, size); // //  sort(x, size);     display<int>(x, size); 

and in display:

template<typename t> void display(t *arr, int size){      cout<<"\nafter sorting: "<<endl;      for(int i=0; i<size; i++){         cout<<arr[i]<<"\t";     } } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -