请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

不能将函数 模板“void selectionSort(T [],int)”用作函数参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    分别定义如下: 
     
    template<typename T>
    void SortTime( string sortName,void(*sort)(T[],int), T arr[],int n)
    {
        clock_t startTime = clock();
        sort(arr ,n);
        clock_t endTime = clock();
 
        assert( isSorted(arr , n));
 
        cout<<sortName << " :" <<double(endTime-startTime) /CLOCKS_PER_SEC <<" s" <<endl;
 
        return;                                                                                                                                                                                                            
    }
    -----------------------------------------------------------------------------------------------------
        template<typename T>
        void selectionSort(T arr[], int n){
         
            for(int i = 0 ; i < n ; i ++){
         
                int minIndex = i;
                forint j = i + 1 ; j < n ; j ++ )
                    if( arr[j] < arr[minIndex] )
                        minIndex = j;
         
                swap( arr[i] , arr[minIndex] );
            }
        }
         
    调用如下:
     
    SortHelper::SortTime("Selection Sort", selectionSort,arr,n);


报错:

1>c:\algorithm\selectionsort\selectionsort\main.cpp(28): error C2896: “void SortHelper::SortTime(std::string,void (__cdecl *)(T [],int),T [],int)”: 不能将函数 模板“void selectionSort(T [],int)”用作函数参数

1>          c:\algorithm\selectionsort\selectionsort\main.cpp(8) : 参见“selectionSort”的声明

1>c:\algorithm\selectionsort\selectionsort\main.cpp(28): error C2784: “void SortHelper::SortTime(std::string,void (__cdecl *)(T [],int),T [],int)”: 未能从“重载函数类型”为“重载函数类型”推导 模板 参数

请问这个该如何解决 多谢 !

正在回答

插入代码

3回答

_T先生_ 2017-01-17 21:19:47

图片加载失败
这里需要的是一个函数名而不是一个函数模板名,即显示实例化函数模板

4 回复 有任何疑惑可以回复我~
  • 谢谢你的分享 :-)
    回复 有任何疑惑可以回复我~ 2017-01-17 23:51:14
  • 提问者 keyboard2steper #2
    感谢你的解答~
    回复 有任何疑惑可以回复我~ 2017-01-18 10:36:23
  • 这里的selectionSort如果是类的成员函数的话,这里带<int>也调用不通。
    回复 有任何疑惑可以回复我~ 2021-08-24 07:47:53
liuyubobobo 2017-01-17 23:37:23

楼上正解。由于VS编译器不是标准的C++实现,所以一些地方会和课程代码有出入。建议加入课程官方群,群公告中对一些这类地方有提示 :-)

2 回复 有任何疑惑可以回复我~
  • 加入了算法2群,并没有在群公告里找到对应关于VS编译环境需要特别注意的地方
    回复 有任何疑惑可以回复我~ 2017-10-28 07:47:02
  • 抱歉,之前只有一个群。刚刚在二群也添加了:)
    回复 有任何疑惑可以回复我~ 2017-10-28 09:24:24
飞雷神面包 2017-01-24 17:26:44

非常感谢 遇到了同样的问题~~~~

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号