任何人都可以修复此代码中的错误?重载函数的错误
# include<iostream>
using namespace std;
void bubble_sort (int a[], int size);
float bubble_sort (float b[], int size);
int read (int a[], int size);
float read (float b[], int size);
void display (int a[], int size);
void display (float b[], int size);
int main()
{
int const size=10;
int a[10];
int b[10];
read (a,size);
read (b,size);
bubble_sort(a,size);
bubble_sort(b,size);
display(a, size);
display(b, size);
return 0;
}
int read (int a[], int size)
{
cout<<"Enter the integer values:";
for (int i=0; i<10;i++)
cin>>a[i];
}
float read (float b[], int size)
{
cout<<"Enter the float values :";
for (int i=0; i<10;i++)
cin>>b[i];
}
void bubble_sort (int a[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
if (a[j]>a[j + 1])
{
swap = a[j];
a[j] = a[j + 1];
a[j + 1] = swap;
}
}
}
float bubble_sort (float b[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
if (b[j]>b[j + 1])
{
swap = b[j];
b[j] = b[j + 1];
b[j + 1] = swap;
}
}
}
void display (int a[], int size)
{
for (int i=0;i<size;i++)
cout<<a[i]<<" ";
}
void display (float b[], int size)
{
cout<<endl;
for(int i=0 ;i<size; i++)
cout<<b[i]<<" ";
}
编译器显示重载函数没有上下文类型信息。但我需要使用重载函数。我怎么解决这个问题?
In function ‘void bubble_sort(int*, int)’:
error: overloaded function with no contextual type information
swap = a[j];
^
error: cannot resolve overloaded function ‘swap’ based on conversion to type ‘int’
a[j + 1] = swap;
尝试函数模板代替 – taocp
_'compiler显示重载函数没有上下文类型信息'_它是怎么回事?错误文本? –
你正在使用'swap'并且你还没有声明它。碰巧有一个'std :: swap'函数模板。 – chris