简体   繁体   中英

in c++ expected primary expression before ']'

Here is my code for quick sort. I am a beginner kindly please help.

#include<iostream>
using namespace std;

class quick
{
    private:
        int n,left,right,i,j;
        float a[55];
    public:
        void getdata();
        void sort(float[],int,int);
        void putdata();

};
void quick::getdata()
{
    cout<<"Enter how many elements you want to enter:";
    cin>>n;

    for(int k=0;k<n;k++)
    {
        cout<<"Enter percentage of students:"<<k+1<<":";
        cin>>a[k];
    }   

    left=0;
    right=n-1;
}

void quick::putdata()
{

    for(int k=0;k<5;k++)
    {
        cout<<"\nSorted marks are:"<<a[k]<<endl;
    }   
}

void quick::sort(float a[],int left,int right)
{
    if(left<right)
    {
        int i=left;
        int j=right+1;
        float pivot=a[left];
        do{
            do{
                i++;
            }while((a[i]<pivot)&& left<right);

            do{
                j--;
            }while(a[j]>pivot);

            if(i<j)
                swap(a[i],a[j]);
        }while(i<j);

        a[left]=a[j];
        a[j]=pivot;
        sort(a,left,j-1);
        sort(a,j+1,right);
    }   
}

int main()
{   

    quick obj;
    obj.getdata();
    obj.sort(a[],left,right);
    obj.putdata();
return (0);
}

It is giving me error in int main() function:

  1. a is not declared in this scope.
  2. expected primary expression before ']'.

As it mentions you have not declared a as a variable inside the int main() . Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a , left , right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a , obj.left , obj.right inside sort function.

Complete code:

#include<iostream>
using namespace std;

class quick
{
     public:
        int n,left,right,i,j;
        float a[55];

        void getdata();
        void sort(float[],int,int);
        void putdata();

};
void quick::getdata()
{
    cout<<"Enter how many elements you want to enter:";
    cin>>n;

    for(int k=0;k<n;k++)
    {
        cout<<"Enter percentage of students:"<<k+1<<":";
        cin>>a[k];
    }   

    left=0;
    right=n-1;
}

void quick::putdata()
{

    for(int k=0;k<5;k++)
    {
        cout<<"\nSorted marks are:"<<a[k]<<endl;
    }   
}

void quick::sort(float a[],int left,int right)
{
    if(left<right)
    {
        int i=left;
        int j=right+1;
        float pivot=a[left];
        do{
            do{
                i++;
            }while((a[i]<pivot)&& left<right);

            do{
                j--;
            }while(a[j]>pivot);

            if(i<j)
                swap(a[i],a[j]);
        }while(i<j);

        a[left]=a[j];
        a[j]=pivot;
        sort(a,left,j-1);
        sort(a,j+1,right);
    }   
}

int main()
{   

    quick obj;
    obj.getdata();
    obj.sort(obj.a,obj.left,obj.right);
    obj.putdata();
return (0);
}

As the answer is given by @Shubham Khatri. Here is the corrected code.

#include<iostream>
using namespace std;
class quick
{
    public: int n,left,right,i,j;
        float a[55];
    public:
        void getdata();
        void sort(float[],int,int);
        void putdata();

};
void quick::getdata()
{
    cout<<"Enter how many elements you want to enter:";
        cin>>n;
    for(int k=0;k<n;k++)
    {
        cout<<"Enter percentage of students:"<<k+1<<":";
        cin>>a[k];
    }   

    left=0;
    right=n-1;
}
void quick::putdata()
{

    for(int k=0;k<n;k++)
    {
        cout<<"\nSorted marks are:"<<a[k]<<endl;

    }   

}
void quick::sort(float a[],int left,int right)
{
    if(left<right)
        {
            int i=left;
            int j=right+1;
            float pivot=a[left];
    do{
        do{
            i++;
              }while((a[i]<pivot)&& left<right);

        do{
            j--;
          }while(a[j]>pivot);

        if(i<j)
            swap(a[i],a[j]);
    }

        while(i<j);
    a[left]=a[j];
    a[j]=pivot;
    sort(a,left,j-1);
    sort(a,j+1,right);
}   
}
int main()
{   

    quick obj;
    obj.getdata();
    obj.sort(obj.a,obj.left,obj.right);
    obj.putdata();
return (0);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM