#include <iostream>
using namespace std;
class sort_overload
{
int a[10];
public:
void sort(int ,int);
void display();
void getdata();
}s;
void sort_overload::sort(int first,int last) //quick sorting
{
int sort_overload :: *sp = &sort_overload :: a[0]; //pointer to the base address of a present in class sorted_overload
sort_overload *sp_obj = &s;
int pivot = sp_obj->*(sp+last);
int i=first,j=last;
int temp;
if(i<j)
{
while((sp_obj->*(sp+i)) < pivot)
i++;
while((sp_obj->*(sp+j)) > pivot)
j--;
if(i<j)
{
temp = (sp_obj->*(sp+i));
(sp_obj->*(sp+i)) = (sp_obj->*(sp+j));
sp_obj->*(sp+j) = pivot;
sp_obj->*(sp+last)=temp;
}
}
s.sort(first,j-1);
s.sort(j+1,last);
}
void sort_overload::display()
{
int sort_overload :: *sp = &sort_overload :: a;
sort_overload *sp_obj = &s;
for(int i=0;i<10;i++)
cout<<"Sorted array numbers:"<<sp_obj->*(sp+i)<<endl;
}
void sort_overload::getdata()
{
int sort_overload:: *sp = &sort_overload :: a;
sort_overload *sp_obj = &s;
cout<<"Enter numbers";
for(int i=0;i<10;i++)
cin>>sp_obj->*(sp+i);
}
int main()
{
s.getdata();
s.sort(0,9);
s.display();
return 0;
}
This statement gives me an error:
int sort_overload :: *sp = &sort_overload :: a[0];
Could somebody tell me why the error is coming up?
I'm trying to sort 10 integer values, and further overload it to sort double and float values as well, but I am stuck on the sorting part.
Since
void sort_overload::sort(int first,int last)
is a member function of your class, you can can access your array by replacing
int sort_overload :: *sp = &sort_overload :: a[0];
by
int *sp = this->a;
or better just use this->a
instead of sp
You cannot use the syntax :
&sort_overload :: a[0];
because your array is not a static attribute.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.