請問這一個程式ERROR該怎麼解決><
簡單說明:利用模板化的副程式,設計不同型態的陣列,將陣列元素藉由模版化的
副程式由小到大排列。
#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
05/22 11:49, 1F
→
05/22 11:49, , 2F
05/22 11:49, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章