主题:类外模板成员函数定义方式,有点费解的定义,不知道如何解决
#include<iostream>
using namespace std;
const int maxsize = 10;
template<typename T>
class search
{
private:
T array[maxsize];
public:
search();
search(T a[]);
~search();
void dubble_sort();
void insert_sort();
void binary_sort();
void sort_choice();
void display();
};
template<typename T>
search<T>::search()
{
array = new T[maxsize];
}
template<typename T>
search<T>::search(T a[])
{
array = new T[maxsize];
for(int i = 0; i < maxsize; i++)
{
array[i] = a[i];
}
}
template<typename T>
search<T>::~search()
{
delete []array;
}
template<typename T>
void search<T>::dubble_sort()
{
for(int i = 1; i < maxsize; i++)
{
for(int j = 0; j <= maxsize-i; j++)
if(array[j] > array[j+1])
{
T temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
template<typename T>
void search<T>::insert_sort()
{
for(int i = 0; i < maxsize; i++)
{
int k = i;
for(int j = i-1; j >= 0; j--)
{
if(array[k] < array[j] )
{
T temp;
temp = array[j];
array[j] = array[k];
array[k] = temp;
}
}
}
}
template<typename T>
void search<T>::binary_sort()
{
for(int i = 0; i < maxsize; i++)
{ int k = i+1;
int left = 0,right = i,mid = (left+right)/2;
if(array[k] > array[mid])
{
left = mid;
mid = (left+right)/2;
}
else
{
right = mid;
mid = (left+right)/2;
}
T temp;
temp = array[mid];
array[mid] = array[k];
array[k] = temp;
}
}
template<typename T>
void search<T>::display()
{
for(int i = 0; i < maxsize; i++)
{
cout << array[i] << "\t";
}
}
template<typename T>
void search<T>::sort_choice()
{
int a;
cin >> a;
cout << "****** 请选择你喜欢的排序方法(0~3): ******" << "\n"
cout << "0: -------- 冒泡排序 -------" << "\n"
<< "1 -------- 直接插入排序 ----- << "\n"
<< "2 -------- 折半插入 ------- << "\n"
<< "3 -------- 退出选择 ------- << "\n";
switch(a)
{
case 0:
this->dubble_sort();
this->display();
break;
case 1:
this->insert_sort();
this->display();
break;
case 2:
this->binary_sort();
this->display();
break;
case 3:
exit(1);
default :
cout << "不好意思,你的输入有误..." << endl;
}
}
#include <cstdlib>
int main(int argc, char *argv[])
{
int a[maxsize];
cout << "请输入" << maxsize << "个整数:"<< endl;
for(int i = 0; i < maxsize; i++)
cin >> a[i];
search <int> my_search(a);
my_search.sort_choice();
system("PAUSE");
return EXIT_SUCCESS;
}问题补充:
我编译了一下,其中有点错误很是费解 :23 C:\Users\Administrator\Desktop\search0.cpp expected constructor, destructor, or type conversion before '<' token
错误指示地点是: search<T>::search()
(当然其余的类外模板定义也是一样的错误)
麻烦各位看看哈,先谢过了...
using namespace std;
const int maxsize = 10;
template<typename T>
class search
{
private:
T array[maxsize];
public:
search();
search(T a[]);
~search();
void dubble_sort();
void insert_sort();
void binary_sort();
void sort_choice();
void display();
};
template<typename T>
search<T>::search()
{
array = new T[maxsize];
}
template<typename T>
search<T>::search(T a[])
{
array = new T[maxsize];
for(int i = 0; i < maxsize; i++)
{
array[i] = a[i];
}
}
template<typename T>
search<T>::~search()
{
delete []array;
}
template<typename T>
void search<T>::dubble_sort()
{
for(int i = 1; i < maxsize; i++)
{
for(int j = 0; j <= maxsize-i; j++)
if(array[j] > array[j+1])
{
T temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
template<typename T>
void search<T>::insert_sort()
{
for(int i = 0; i < maxsize; i++)
{
int k = i;
for(int j = i-1; j >= 0; j--)
{
if(array[k] < array[j] )
{
T temp;
temp = array[j];
array[j] = array[k];
array[k] = temp;
}
}
}
}
template<typename T>
void search<T>::binary_sort()
{
for(int i = 0; i < maxsize; i++)
{ int k = i+1;
int left = 0,right = i,mid = (left+right)/2;
if(array[k] > array[mid])
{
left = mid;
mid = (left+right)/2;
}
else
{
right = mid;
mid = (left+right)/2;
}
T temp;
temp = array[mid];
array[mid] = array[k];
array[k] = temp;
}
}
template<typename T>
void search<T>::display()
{
for(int i = 0; i < maxsize; i++)
{
cout << array[i] << "\t";
}
}
template<typename T>
void search<T>::sort_choice()
{
int a;
cin >> a;
cout << "****** 请选择你喜欢的排序方法(0~3): ******" << "\n"
cout << "0: -------- 冒泡排序 -------" << "\n"
<< "1 -------- 直接插入排序 ----- << "\n"
<< "2 -------- 折半插入 ------- << "\n"
<< "3 -------- 退出选择 ------- << "\n";
switch(a)
{
case 0:
this->dubble_sort();
this->display();
break;
case 1:
this->insert_sort();
this->display();
break;
case 2:
this->binary_sort();
this->display();
break;
case 3:
exit(1);
default :
cout << "不好意思,你的输入有误..." << endl;
}
}
#include <cstdlib>
int main(int argc, char *argv[])
{
int a[maxsize];
cout << "请输入" << maxsize << "个整数:"<< endl;
for(int i = 0; i < maxsize; i++)
cin >> a[i];
search <int> my_search(a);
my_search.sort_choice();
system("PAUSE");
return EXIT_SUCCESS;
}问题补充:
我编译了一下,其中有点错误很是费解 :23 C:\Users\Administrator\Desktop\search0.cpp expected constructor, destructor, or type conversion before '<' token
错误指示地点是: search<T>::search()
(当然其余的类外模板定义也是一样的错误)
麻烦各位看看哈,先谢过了...