模板类外定义成员函数出错了,请问如何修改,源代码如下
#include<iostream>usingnamespacestd;constintmaxsize=10;template<typenameT>classsearch...
#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()
(当然其余的类外模板定义也是一样的错误)
麻烦各位看看哈,先谢过了... 展开
3个回答
展开全部
你的代码我运行过了 没有你说的那个错误,但你代码中有很多错误 几个排序都不能正确排出 下面是我给你改后的 在我机子上能运行 你在看看 不会的可以问我
#include<iostream>
using namespace std;
const int maxsize = 10;
template<typename T>
class search
{
private:
T *array;
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()
{
cout << "****** 请选择你喜欢的排序方法(0~3): ******" << "\n";
cout << "0: -------- 冒泡排序 -------" << "\n"
<< "1 -------- 直接插入排序 ----- "<< "\n"
<< "2 -------- 折半插入 ------- " << "\n"
<< "3 -------- 退出选择 ------- " << "\n";
int a;
cin >> a;
switch(a)
{
case 0:
dubble_sort();
display();
break;
case 1:
insert_sort();
display();
break;
case 2:
binary_sort();
display();
break;
case 3:
exit(1);
break;
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;
}
#include<iostream>
using namespace std;
const int maxsize = 10;
template<typename T>
class search
{
private:
T *array;
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()
{
cout << "****** 请选择你喜欢的排序方法(0~3): ******" << "\n";
cout << "0: -------- 冒泡排序 -------" << "\n"
<< "1 -------- 直接插入排序 ----- "<< "\n"
<< "2 -------- 折半插入 ------- " << "\n"
<< "3 -------- 退出选择 ------- " << "\n";
int a;
cin >> a;
switch(a)
{
case 0:
dubble_sort();
display();
break;
case 1:
insert_sort();
display();
break;
case 2:
binary_sort();
display();
break;
case 3:
exit(1);
break;
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;
}
追问
先前那个基本的错误解决了,是我编译器有问题,可是你改的这个运行结果也不对啊
追答
嗯 是不对 是你写的方法不对 不要奢求别人给你改的完全正确 自己要动手去写 正真明白了 才有提高 望你能自己动手去改
展开全部
[1] 类模板定义的时候要把类函数的实现部分和定义部分都放在 .h文件里面, 不能放在 .cpp里面,
[2] 如果非要放cpp里面, 那么就要在main.cpp里 加入以下两个, 缺一不可.
#include "search.h"
#include "search.cpp"
[2] 如果非要放cpp里面, 那么就要在main.cpp里 加入以下两个, 缺一不可.
#include "search.h"
#include "search.cpp"
追问
用的是dev-c++ ,我换用vc6.0只有你说的那个array = new T[maxsize]有错误,通过查找在array前加了 T * 可以编译通过,可是得不到结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
模板类外定义成员函数没有问题。不过其它地方倒是有些错误,比如,数据成员array为数组,构造函数中的
array = new T[maxsize];
析构函数中的
delete[] array;
还有,成员函数sort_choice()中的一些小错误。
array = new T[maxsize];
析构函数中的
delete[] array;
还有,成员函数sort_choice()中的一些小错误。
更多追问追答
追问
可是编译提示错误是:23 C:\Users\Administrator\Desktop\search0.cpp expected constructor, destructor, or type conversion before '::search()
{以上为其中一点}
那个array = new T[maxsize];错在哪里啊?
追答
array是数组名,怎么还要分配内存啊。
不应该有错的,不知道你用的是什么编译器。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询