我的排序代码有错误

时间:2013-10-15 12:38:19

标签: c++ arrays

我有bsort(array[],a] has a 69 C:\Users\heide\Desktop\Exam.cpp expected primary-expression before ']' token的错误 也是search(array[],a,b)

你可以帮我完成这个吗?这是代码:

#include<iostream>
using namespace std;

struct Sort{

   void search(int array[],int x,int y)
   {
      for(int i=0;i<=x;i++)
      {
         if(array[i]==y)
         {
            cout<<"The number you enter appeared on array number "<<i<<endl;
         }
         else
         {
            cout<<"ERROR"<<endl;
         }
      }
   }
   void swap(int *x,int *y)
   {
      int i=0;
      i=*x;
      *x=*y;
      *y=i;
   }
   void bsort(int array[],int x)
   {
       for(int a=0;a<x-1;a++)
       {
           for(int b=0;b<x-1;b++)
           {
               if(array[a]> array[b])
               {
                  swap(&array[a],&array[b]);
                  for(int i=0;i<=x;i++)
                  {
                     cout<<array[i]<<"\t";
                  }
                  cout<<endl;
               }
           }
       }
   }
};

int main()
{
   Sort sort;
   int a;
   int b;
   cout<<"Enter the number of elements in the array "<<endl;
   cin>>a;
   int array[a];
   for(int i=0; i<=a;i++)
   {
      cout<<"Enter Number for Element "<<i<<": "<<endl;
      cin>>array[i];
   }
   cout<<"The List Before Sorting"<<endl;
   for(int i=0;i<=a;i++)
   {
      cout<<array[i]<<"\t"<<endl;
   }
   cout<<"Elements Will is Sorting....."<<endl;
   sort.bsort(array[],a);
   cout<<"Enter The Number Needed to be Searched "<<endl;
   cin>>b;
   sort.search(array[],a,b);
   system("pause");
   return 0;
}

1 个答案:

答案 0 :(得分:3)

在调用bsort函数时删除方括号[]。它应该是

sort.bsort(array,a);