請問這一個程式ERROR該怎麼解決><

看板C_and_CPP (C/C++)作者 (no元)時間16年前 (2009/05/22 10:53), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
簡單說明:利用模板化的副程式,設計不同型態的陣列,將陣列元素藉由模版化的 副程式由小到大排列。 #include<iostream> #include<string> using namespace std; template<class T> void search(const T a[],T lowend,T highend,T key,bool& found,T& location) { T first=lowend; T last=highend; T mid; found = false; while ( (first <= last) && !(found) ) { mid = (first + last)/2; if (key == a[mid]) { found = true; location = mid; } else if (key < a[mid]) { last = mid - 1; } else if (key > a[mid]) { first = mid + 1; } } }; template<class T> void swap(T& a,T& b) { T temp; temp=a; a=b; b=temp; }; template<class T> void sort(T a[],T numberused) { int smallest; for(int i=0;i<numberused;i++){ smallest=smallestindex(a,i,numberused); swap(a[i],a[smallest]); } }; template<class T> int smallestindex(T a[],int smallIndex,int numberused) { T min=a[smallIndex]; for(int i=smallIndex+1;i<numberused;i++){ if(a[i]<min) min=a[i]; smallIndex=i; } return smallIndex; }; int main() { const int length=10; int int_array[length],key1,location,index=length-1; char char_array[length],key2; bool found; cout<<"輸入"<<length<<"個整數:"; for(int i=0;i<length;i++) cin>>int_array[i]; sort(int_array,length); for(int i=0;i<length;i++) cout<<int_array[i]; cout<<"輸入尋找的數:"; cin>>key1; search(int_array,0,index,key1,found,location); cout<<"目標位於"<<location<<"指標上"; system("pause"); } 編譯器編譯出來的結果出現一個ERROR!如下 nstantiated from here 舉個例子來說(第66行) call of overloaded `swap(int&, int&)' is ambiguous sort的呼叫意義不清楚! candidates are: void swap(T&, T&) [with T = int]可能是swap的問題 void std::swap(_Tp&, _Tp&) [with _Tp = int] -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.181.63

05/22 11:49, , 1F
你衝到std裡的標準函式了= =
05/22 11:49, 1F

05/22 11:49, , 2F
無意識地使用 using namespace std 的下場
05/22 11:49, 2F
文章代碼(AID): #1A5XE-67 (C_and_CPP)
文章代碼(AID): #1A5XE-67 (C_and_CPP)