小弟定义个函数
编译时报错:error C2664: is_right : cannot convert parameter 1 from int to const int []
我就看不出哪里有类型转换
………
Fraction Math24_showall::jisuan(Fraction opc1,int op,Fraction opc2)
{
switch(op)
{
case 1:
opc1+=opc2;
break;
case 2:
opc1-=opc2;
break;
case 4:
opc1*=opc2;
break;
case 5:
opc1/=opc2;
break;
};
return opc1;
}
bool Math24_showall::is_right(const int op[3],Fraction opc[4],int type)
{
bool equal24=0;
switch (type)
{
case 1: //(a op0 b) op1 (c op2 d)
if (jisuan(jisuan(opc[0],op[0],opc[1]),op[1],jisuan(opc[2],op[2],opc[3]))==24) equal24=1;
break;
case 2: //((a op0 b) op1 c) op2 d
if (jisuan(jisuan(jisuan(opc[0],op[0],opc[1]),op[1],opc[2]),op[2],opc[3])==24) equal24=1;
break;
case 3: //(a op0 (b op1 c)) op2 d
if (jisuan(jisuan(opc[0],op[0],jisuan(opc[1],op[1],opc[2])),op[2],opc[3])==24) equal24=1;
break;
case 4: //a op0 ((b op1 c) op2 d)
if (jisuan(opc[0],op[0],jisuan(jisuan(opc[1],op[1],opc[2]),op[2],opc[3]))==24) equal24=1;
break;
case 5: //a op0 (b op1 (c op2 d))
if (jisuan(opc[0],op[0],jisuan(opc[1],op[1],jisuan(opc[2],op[2],opc[3])))==24) equal24=1;
break;
};
return equal24;
}
void Math24_showall::getString(const int op[3],const Fraction opc[4],int type,buffer* buf)
{
……
if (is_right(op[3],opc[4],type))//-->报错的地方
…………………
if (is_right(op[3],opc[4],type))//-->报错的地方
这里的op[3]是一个数不是数组
like this:
void fn( a[4],...)
int a[4];
fn(a[4],...)//error!!
is_right的第二个参数也是同样的问题!!
bool Math24_showall::is_right(const int op[3],Fraction opc[4],int type)
前两个参数为指针
if (is_right(op[3],opc[4],type))//-->报错的地方
这里前两个参数为int
op[3]是int
const int op[3]是const int *
当然不行
可以用is_right(op,opc,type)
参数类型不匹配
bool Math24_showall::is_right(const int op[3],Fraction opc[4],int type)
事实上定义了等同于这样的函数:
bool Math24_showall::is_right(const int *,Fraction *,int type)
当数组作为参数传递递时,退化为指针
在参数传递的过程中,你最好用指针来完成,你可以将if (is_right(op[3],opc[4],type))//中的数组改为指针,应该就不会报错了。