2011-07-08 36 views
0

請您幫助我用C++中的這個簡單代碼,我不知道這裏有什麼問題?C++問題,錯誤C2064:術語不會評估爲帶1個參數的函數

#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); 
} 
+0

後至少是錯誤的,並在那裏出現 –

+0

@NAIEM:我建議你開始學習如何處理從錯誤消息中的信息。編譯器可能告訴了你問題的確切位置,這應該限制了搜索。如果您仍然無法理解該行發生了什麼,請發佈一個問題,但不要忘記提供編譯器給您的完整信息(即行號)(在行中添加一條註釋,報告錯誤:'' //編譯器錯誤或者類似的東西) –

回答

1

table(mid)應該是table[mid]

0
if(item==table(mid)) 

應該

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

非常感謝 – NAIEM

1

它必須是 if(item==table[mid])

if(item==table(mid)) 
1

問題是,您很難將[(混淆。取而代之的

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

你需要

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