函数的重载
#include<iostream>
using namespace std;
int min(int a,int n){
int m,i=0;
while(i>=n-1){
if(a[i]<a[i+1])
{
a[i+1]=a[i];
m=a[i+1];
}
i++;
};
return m;
}
double min(double a,int n){
double m;
int i=0;
while(i>=n-1){
if(a[i]<a[i+1])
{
a[i+1]=a[i];
m=a[i+1];
}
i++;
};
return m;
}
float min(float a,int n){
float m;
int i=0;
while(i>=n-1){
if(a[i]<a[i+1])
{
a[i+1]=a[i];
m=a[i+1];
}
i++;
};
return m;
}
long min(long a,int n){
long m;
int i=0;
while(i>=n-1){
if(a[i]<a[i+1])
{
a[i+1]=a[i];
m=a[i+1];
}
i++;
};
return m;
}
void main()
{
int a[3]={1,2,3};
double b[3]={1.14546,2.165465,3.1654};
float c[3]={1.1,2.2,3.3};
long d[3]={1,2,3};
cout << min(a,3) << endl;
cout << min(b,3) << endl;
cout << min(c,3) << endl;
cout << min(d,3);
}
然后出现了一大堆错误
E:\编程\习题\2111.cpp(6) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(6) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(8) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(8) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(8) : error C2106: '=' : left operand must be l-value
E:\编程\习题\2111.cpp(9) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(20) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(20) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(22) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(22) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(22) : error C2106: '=' : left operand must be l-value
E:\编程\习题\2111.cpp(23) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(34) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(34) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(36) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(36) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(36) : error C2106: '=' : left operand must be l-value
E:\编程\习题\2111.cpp(37) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(49) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(49) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(51) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(51) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(51) : error C2106: '=' : left operand must be l-value
E:\编程\习题\2111.cpp(52) : error C2109: subscript requires array or pointer type
E:\编程\习题\2111.cpp(63) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
E:\编程\习题\2111.cpp(63) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
E:\编程\习题\2111.cpp(63) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
E:\编程\习题\2111.cpp(65) : error C2665: 'min' : none of the 4 overloads can convert parameter 1 from type 'int [3]'
E:\编程\习题\2111.cpp(66) : error C2665: 'min' : none of the 4 overloads can convert parameter 1 from type 'double [3]'
E:\编程\习题\2111.cpp(67) : error C2665: 'min' : none of the 4 overloads can convert parameter 1 from type 'float [3]'
E:\编程\习题\2111.cpp(68) : error C2665: 'min' : none of the 4 overloads can convert parameter 1 from type 'long [3]'
[解决办法]
函数参数写错了
int min(int a,int n)
你这样写的a是整型的,不是数组
正确写法
int min(int*a ,int n)