[問題] 字串陣列不能當作引數(argument)傳給fu …

看板C_and_CPP (C/C++)作者 (no元)時間16年前 (2009/05/23 20:35), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
實做一個模組化的程式,該程式會將多種不同型態的陣列 透過模組化的單一副程式將陣列元素排序由小到大。最後輸入某一個元素的值,程式 會輸出該值在哪個指標上以下為程式碼 問題是現在主程式會測是整數陣列和字串陣列,再debug時整數陣列OK 但是字串陣列部分出現以下錯誤訊息 no matching function for call to `search(std::string*&, int, int&, std::string&, bool&, int&)' no matching function for call to `sort(std::string*&, int&)' #include<iostream> #include<string> using namespace std; int main() { const int length=10;/陣列長度 int choice;/選擇要執行哪種型態的陣列 cout<<"選擇測試1.整數陣列 2.字串陣列:"; do{ cin>>choice; if(choice==1){/執行整數型態的陣列 int array1[length]; process(array1,length); }else if(choice==2){/執行字串形態的陣列 string array2[length]; process(array2,length); } }while(choice!=1 && choice !=2); system("pause"); } template<class T> void process(T array[],int length) { T key1; int location,index=length-1; bool found; cout<<"輸入"<<length<<"個整數:"; for(int i=0;i<length;i++) cin>>array[i]; sort(array,length); for(int i=0;i<length;i++) cout<<array[i]; cout<<"輸入尋找的數:"; cin>>key1; search(array,0,index,key1,found,location); cout<<"目標位於"<<location<<"指標上"; }; 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 swap1(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); swap1(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; }; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.181.63 ※ 編輯: satokuzao 來自: 140.117.181.63 (05/23 20:36)

05/23 21:10, , 1F
search(array,0,index,key1,found,location);
05/23 21:10, 1F

05/23 21:10, , 2F
process<T = string> -> search<T = string>
05/23 21:10, 2F

05/23 21:11, , 3F
search<T = string> 把T帶進去參數列裡面就知道錯誤哪來的
05/23 21:11, 3F

05/23 21:12, , 4F
sort也相同道理
05/23 21:12, 4F

05/24 01:06, , 5F
非常感激akasan的解答,不過小弟還是不太懂。
05/24 01:06, 5F

05/24 01:06, , 6F
可以用另一種講法解釋嗎? 先謝過了
05/24 01:06, 6F

05/24 01:56, , 7F
簡單講就是你參數型態根本不對
05/24 01:56, 7F
文章代碼(AID): #1A5-sMSW (C_and_CPP)
文章代碼(AID): #1A5-sMSW (C_and_CPP)