【发布时间】:2018-07-20 07:46:53
【问题描述】:
在“if (size == list[i])”处,“==”上的红色标记为No operator "==" matches these operandsoperand types are: int == InventoryRecord。我没有看到我在这里做错了什么。谁能给我解释一下这是为什么?
void linear_search(InventoryRecord list[], int size) {
int i;
cout << "\nEnter Element to Search : ";
cin >> size;
/* for : Check elements one by one - Linear */
for (i = 0; i < MAX_SIZE; i++) {
/* If for Check element found or not */
if (size == list[i]) {
cout << "\nLinear Search : Element : " << size << " : Found : Position : " << i + 1 << ".\n";
break;
}
}
if (i == MAX_SIZE)
cout << "\nSearch Element : " << size << " : Not Found \n";
}
【问题讨论】:
-
编译器如何知道如何比较 int 和 InventoryRecord?
-
很抱歉,由于缺少许多细节,我们可能也看不到您的所作所为。请提供minimal reproducible example。
-
你为什么认为这可行?
-
您尝试将
int与InventoryRecord进行比较。这很难评论,因为您没有显示InventoryRecord是如何定义的。但是,与int相比似乎不兼容(正如编译器错误所说)。 -
停止使用数组,改用
std::vector。将更容易使用standard algorithm functions,例如std::findorstd::find_if。
标签: c++ function struct linear-search