注册 登录
编程论坛 C++教室

关于模板使用的错误

fishviv 发布于 2011-01-06 23:04, 1618 次点击
#include<iostream>
using namespace std;
template <typename T>
void merge_sort_aux( int * first ,int * last ,int* temp)
{
    int* mid = first +(last - first)/2;
    if(first >= mid)
        return ;
    merge_sort(first , mid , temp);
    merge_sort(mid , first, temp);
    std::merge(temp ,first , mid ,mid ,last);
    std::copy(temp,temp + (last - first ), first);
}
template <typename T>
void merge_sort(int * first ,int * last)
{
    int * temp = new int[last - first];
    merge_sort_aux(first ,last , temp);
    delete[] temp;
}
int main()
{    int *num = new int[6];
    for(int i = 0 ; i < 5 ;++i)
        cin>>num[i];
        merge_sort(num , num+5);
    for(i = 0 ; i < 5 ;++i)
        cout<<num[i]<<"  ";
    cout<<endl;
return 0;
}
11 回复
#2
fishviv2011-01-06 23:07
Compiling...
hate.cpp
f:\cpp\hate.cpp(25) : error C2783: 'void __cdecl merge_sort(int *,int *)' : could not deduce template argument for 'T'
Error executing cl.exe.

hate.obj - 1 error(s), 0 warning(s)
#3
八画小子2011-01-07 01:54
楼主,我没有仔细看你的程序思路,但从代码上,我感觉你还不清楚模板的作用,不知道模板到底是做什么的。而且你的函数参数列表有问题,数目不一致
#4
fishviv2011-01-07 09:17
以下是引用八画小子在2011-1-7 01:54:41的发言:

楼主,我没有仔细看你的程序思路,但从代码上,我感觉你还不清楚模板的作用,不知道模板到底是做什么的。而且你的函数参数列表有问题,数目不一致
恩,是很不懂,看到这个错误了.改过了..但是还不对.改过.
//数组的归并排序,
#include<iostream>
using namespace std;
template <typename T>
void merge_sort_aux( int * first ,int * last ,int* temp)
{
    int* mid = first +(last - first)/2;
    if(first >= mid)
        return ;
    merge_sort_aux(first , mid , temp);
    merge_sort_aux(mid , first, temp);//划分为小段
    std::merge(temp ,first , mid ,mid ,last);//合并
    std::copy(temp,temp + (last - first ), first);//复制到first中
}
template <typename T>
void merge_sort(int * first ,int * last)
{
    int * temp = new int[last - first];
    merge_sort_aux(first ,last , temp);
    delete[] temp;
}
int main()
{    int *num = new int[6] ;
    for(int i = 0 ; i < 5 ;++i)
        cin>>num[i];
        merge_sort(num , num+5);
    for(i = 0 ; i < 5 ;++i)
        cout<<num[i]<<"  ";
    cout<<endl;
    system("pause");
return 0;
}
#5
kspliusa2011-01-07 09:48
以下是引用fishviv在2011-1-7 09:17:09的发言:

恩,是很不懂,看到这个错误了.改过了..但是还不对.改过.
//数组的归并排序,
#include
using namespace std;
template
void merge_sort_aux( int * first ,int * last ,int* temp)
{
    int* mid = first +(last - first)/2;
    if(first >= mid)
        return ;
    merge_sort_aux(first , mid , temp);
    merge_sort_aux(mid , first, temp);//划分为小段
    std::merge(temp ,first , mid ,mid ,last);//合并
    std::copy(temp,temp + (last - first ), first);//复制到first中
}
template
void merge_sort(int * first ,int * last)
{
    int * temp = new int[last - first];
    merge_sort_aux(first ,last , temp);
    delete[] temp;
}
int main()
{    int *num = new int[6] ;
    for(int i = 0 ; i < 5 ;++i)
        cin>>num;
        merge_sort(num , num+5);
    for(i = 0 ; i < 5 ;++i)
        cout<#include <iostream>
#include <algorithm> //添加头文件
using namespace std;
template <typename T>
void merge_sort_aux( T * first ,T * last ,T* temp)
{
    T* mid = first +(last - first)/2;
    if(first >= mid)
        return ;
    merge_sort_aux(first , mid , temp);
    merge_sort_aux(mid, first, temp);//划分为小段,mid add 1
    std::merge(temp ,first , mid ,mid,last);//合并
    std::copy(temp,temp + (last - first ), first);//复制到first中
}
template <typename T>
void merge_sort(T * first ,T * last)
{
    T * temp = new T [last - first];
    merge_sort_aux(first ,last , temp);
    delete[] temp;
}
int main()
{   int *num = new int[6] ;
    for(int i = 0 ; i < 5 ;++i)
        cin>>num[i];
    merge_sort(num , num+5);
    for(int i = 0 ; i < 5 ;++i) // 这里的i要定义,估计你用的是vc6.0,所以没用定义
        cout<<num[i]<<"  ";
    cout<<endl;
    system("pause");
    delete [] num; // 这里释放内存
return 0;
}

改了改,可是不能运行,可能是lz写的算法有问题!
#6
sunmingchun2011-01-07 12:21
先学习了!
#7
CCFzeroOH2011-01-07 19:13
你定义模板没有什么用啊!
#8
pangding2011-01-08 01:20
把所有的带 template <..> 的行删掉可能就行了。
#9
fishviv2011-01-08 11:03
以下是引用CCFzeroOH在2011-1-7 19:13:42的发言:

你定义模板没有什么用啊!
想用STL里面的函数,不用模板的我会,再添一个merge函数..
#10
fishviv2011-01-08 11:04
以下是引用pangding在2011-1-8 01:20:29的发言:

把所有的带 template <..> 的行删掉可能就行了。
那样就会说:merge函数没有定义了吧?
#11
fishviv2011-01-08 11:06
以下是引用kspliusa在2011-1-7 09:48:26的发言:


改了改,可是不能运行,可能是lz写的算法有问题!
不用模板,再添一个merge函数,就好了,我用的是VC.6
#12
lhnupsud2011-01-09 07:13
你定义的模板函数参数列表里面怎么没有用到泛型T呢?你写的函数根本没作用啊。
1