In main function of selection sorting of array it is showing error that no matching function found

Stuti Thakur 41 Reputation points
2021-10-20T17:26:19.47+00:00

include <iostream>

using namespace std;
class sort{
public:
int size;
sort()
{
cout<<"Enter the size of array for sorting";
cin>>size;
}
};
template <class T>
class Selection:public sort
{
public:
int *Array=new int[size];
void swap(int *a,int *b);
void printArray(int *Array[], int size);
void selectionSort(int *Array[], int size);
Selection()
{
cout<<"Enter the array for sorting";
for(int i=0;i<size;i++)
{
cin>>Array[i];
}

 }

};
template <class T>
void Selection<T>::swap(int *a, int *b) // function to swap the the position of two elements
{
int temp = *a;
*a = *b;
*b = temp;
}
template <class T>
void Selection<T>::printArray(int *Array[], int size) // function to print an array
{
for (int i = 0; i < size; i++)
{
cout << Array[i] << " ";
}
cout << endl;
}
template <class T>
void Selection<T>::selectionSort(int *Array[], int size) {

for (int step = 0; step < size - 1; step++) {
int min = step;
for (int i = step + 1; i < size; i++)
{
if (Array[i] < Array[min]) // To sort in descending order, change > to < in this line.
min = i; // Select the minimum element in each loop.
}
swap(&Array[min], &Array[step]); // put minimum value at the correct position
printArray(Array,size) ;
}
}
int main() // driver code
{
Selection<int>l;
l.selectionSort(l.Array,l.size);
cout << "Sorted array in Acsending Order:\n";
l.printArray(l.Array,l.size);
}

Developer technologies C++
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2021-10-21T05:51:29.68+00:00

    Hi @Stuti Thakur ,

    For point-related knowledge, you need to review it again. The array you are sorting is an integer one-dimensional array, so the function parameter should also be an array of the same type. I modified the code you provided, now the code can realize the sorting function, you could refer to it.

    By the way , you could use this function when uploading the code, which will help everyone understand your code more easily.

    142353-qwer.png

    #include <iostream>  
    using namespace std;  
    class sort {  
    public:  
    	int size;  
    	sort()  
    	{  
    		cout << "Enter the size of array for sorting";  
    		cin >> size;  
    	}  
    };  
    template <class T>  
    class Selection :public sort  
    {  
    public:  
    	int* Array = new int[size];  
    	void swap(int* a, int* b);  
    	void printArray(int* Array, int size);  
    	void selectionSort(int* Array, int size);  
    	Selection()  
    	{  
    		cout << "Enter the array for sorting";  
    		for (int i = 0; i < size; i++)  
    		{  
    			cin >> Array[i];  
    		}  
    	}  
    };  
    template <class T>  
    void Selection<T>::swap(int* a, int* b) // function to swap the the position of two elements  
    {  
    	int temp = *a;  
    	*a = *b;  
    	*b = temp;  
    }  
    template <class T>  
    void Selection<T>::printArray(int* Array, int size) // function to print an array  
    {  
    	for (int i = 0; i < size; i++)  
    	{  
    		cout << Array[i] << " ";  
    	}  
    	cout << endl;  
    }  
    template <class T>  
    void Selection<T>::selectionSort(int* Array, int size) {  
      
    	for (int step = 0; step < size - 1; step++) {  
    		int min = step;  
    		for (int i = step + 1; i < size; i++)  
    		{  
    			if (Array[i] < Array[min]) // To sort in descending order, change > to < in this line.  
    				min = i; // Select the minimum element in each loop.  
    		}  
    		swap(&Array[min], &Array[step]); // put minimum value at the correct position  
    	}  
    }  
    int main() // driver code  
    {  
    	Selection<int>l;  
    	l.selectionSort(l.Array, l.size);  
    	cout << "Sorted array in Acsending Order:\n";  
    	l.printArray(l.Array, l.size);  
    }  
    

    142317-asd.png

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.