所以,我正在构建一个项目,并且出现错误:
no viable conversion from returned value of type bankAccount to function return type int
|111|error: invalid operands to binary expression ('bankAccount' and 'int')|
int bankAccount::searchfor(bankAccount lists[], int length, int acctNum)
{
for(int i = 0; i < length; i++)
{
if(lists[i] == acctNum)
{
return lists[i];
}
else
return -1;
}
}
答案 0 :(得分:3)
在bankAccount::searchfor
中,函数的返回类型为int
,但返回类型为lists[i]
的{{1}}。
猜测,您打算返回bankAccount
:
i
但是,看来您的意思是如果找到int
bankAccount::searchfor(bankAccount lists[], int length, int acctNum) {
for(int i = 0; i < length; i++) {
if(lists[i] == acctNum) {
return i;
} else
return -1;
}
}
则返回i
,否则返回acctNum
。因此,将-1
移到循环未找到return -1
的情况下返回的值。
acctNum