C++问题,错误C2064:术语的计算结果不是采用1个参数的函数

C++ Question,error C2064: term does not evaluate to a function taking 1 arguments

本文关键字:函数 参数 1个 结果 计算 问题 错误 C2064 术语 C++      更新时间:2023-10-16

你能帮我用C++编写这个简单的代码吗?我不知道这里有什么问题?

#include <iostream>
#include <string>
using namespace::std;
template <class Type>
Type binsearch (Type item,Type *table,Type n)
{
    int bot=0;
    int top=n-1;
    int mid, cmp;
    while (bot<= top)
    {
        mid=(bot+top)/2;
        if(item==table(mid))
            return (mid);
        else if (item <table[mid])
            top=mid-1;
        else
            bot=mid+1;
    }
    return -1;
}
int main ()
{
    int nums[]={10, 12, 30, 38, 52, 100};
    cout<< binsearch(52, nums, 6);
}

table(mid)应该是table[mid]

问题是您混淆了[(。代替

---
mid=(bot+top)/2;
if(item==table(mid))
    return (mid);
---

你需要

+++
mid=(bot+top)/2;
if(item==table[mid])
    return (mid);
+++

它必须if(item==table[mid])

不是

if(item==table(mid))
if(item==table(mid))

应该是

if(item==table[mid]) //notice square brackets []
              ^   ^