提问者:小点点

冒泡排序程序中未声明错误函数


我在编译bubblesort程序时遇到了一些问题,它给了我错误:“bubblesort”没有在bubblesort(a,5)作用域中声明;

#include<iostream>
using namespace std;
int main()
{
  int a[]={12,34,8,45,11};
  int i;
  bubblesort(a,5);
  for(i=0;i<=4;i++)
  cout<<a[i];
}

void bubblesort(int a[],int n)
{
  int round,i,temp;
  for(round=1;round<=n-1;round++)
  for(i=0;i<=n-1-round;i++)
  if(a[i]>a[i+1])
 {
   temp=a[i];
   a[i]=a[i+1];
   a[i+1]=temp;
  }
}

共1个答案

匿名用户

在C++中,词法顺序很重要,也就是说,如果您使用一个名称,那么在使用之前必须至少声明该名称。 (当然也可以在使用之前定义)。

所以你需要:

void bubblesort(int a[],int n); // declare

int main()
{
  // ...
  bubblesort(a,5);   // use
}

void bubblesort(int a[],int n)  // define
{
 // ...
}

void bubblesort(int a[],int n)  // define
{
 // ...
}

int main()
{
  // ...
  bubblesort(a,5);   // use
}