为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

	分别定义如下: 
	
	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;
                for( int 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)”: 未能从“重载函数类型”为“重载函数类型”推导 模板 参数
	
        调用修改为SortHelper::SortTime("Selection Sort", selectionSort<int>,arr,n);后,再次报错
        MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup1>F:\C++程序\Selection_Sort(2)\Debug\Selection_Sort(2).exe : fatal error LNK1120: 1 unresolved externals
        请问是为什么呢?


正在回答

1 回答

已解决,我把控制台应用程序建成控制台程序了,不好意思多有打扰

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之模板篇
  • 参与学习       91174    人
  • 解答问题       318    个

本C++教程力求即学即会,所有知识以实践方式讲解到操作层面

进入课程

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信