| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1618 人关注过本帖
标题:关于模板使用的错误
只看楼主 加入收藏
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:11 
关于模板使用的错误
#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;
}
搜索更多相关主题的帖子: void include return 
2011-01-06 23:04
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
收藏
得分:0 
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)
2011-01-06 23:07
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:3 
楼主,我没有仔细看你的程序思路,但从代码上,我感觉你还不清楚模板的作用,不知道模板到底是做什么的。而且你的函数参数列表有问题,数目不一致
2011-01-07 01:54
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
收藏
得分:0 
以下是引用八画小子在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;
}
2011-01-07 09:17
kspliusa
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:98
专家分:178
注 册:2009-9-27
收藏
得分:3 
以下是引用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写的算法有问题!
2011-01-07 09:48
sunmingchun
Rank: 4
来 自:安徽-滁州
等 级:业余侠客
帖 子:198
专家分:277
注 册:2010-4-2
收藏
得分:3 
先学习了!
2011-01-07 12:21
CCFzeroOH
Rank: 2
等 级:论坛游民
帖 子:79
专家分:85
注 册:2009-12-22
收藏
得分:3 
你定义模板没有什么用啊!
2011-01-07 19:13
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:3 
把所有的带 template <..> 的行删掉可能就行了。
2011-01-08 01:20
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
收藏
得分:0 
以下是引用CCFzeroOH在2011-1-7 19:13:42的发言:

你定义模板没有什么用啊!
想用STL里面的函数,不用模板的我会,再添一个merge函数..
2011-01-08 11:03
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
收藏
得分:0 
以下是引用pangding在2011-1-8 01:20:29的发言:

把所有的带 template <..> 的行删掉可能就行了。
那样就会说:merge函数没有定义了吧?
2011-01-08 11:04
快速回复:关于模板使用的错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015256 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved