| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3286 人关注过本帖
标题:函数指针
只看楼主 加入收藏
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
结帖率:100%
收藏
 问题点数:0 回复次数:15 
函数指针
这样定义函数指针的:
typedef void (*Func)(long c,long s);
static Func Sort[SORTNUM]={
    BubbleSort,InsertSort,SelectSort,QuickSort,ShellSort,HeapSort};
然后调用时:(*Sort[j])(c,s);

编译时出现如下错误:
error: `Sort' undeclared (first use this function)
error: (Each undeclared identifier is reported only once for each function it appears in.)*/
望高手指点一下
搜索更多相关主题的帖子: 函数指针 Sort function Func 
2008-06-27 12:42
界水乘风
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-27 12:52
onlyonegod
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2008-6-25
收藏
得分:0 
我想Func是你定义的函数指针,你想用typedef定义了一个新的类型(函数指针类型),但是我认为
应该在typedef这条语句之后加上一个新类型的名称,例如FuncType。
然后再static FuncType Sort[SORTNUM]={
    BubbleSort,InsertSort,SelectSort,QuickSort,ShellSort,HeapSort};

不知道这样对不对
2008-06-27 13:00
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
我觉得应该是这样调用
(Sort[j])(c,s);
你试试看

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-06-27 13:11
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
收藏
得分:0 
我贴下代码,大家帮忙看下
#ifndef OALIST_H //no macro name given in #ifndef directive    
    #define OALIST_H        
#include<stdlib.h>
#define MAXSIZE 1000
#define SORTNUM 6
#define FALSE 0
#define TRUE 1

typedef void (*Func)(long c,long s);
static Func Sort[SORTNUM]={
    BubbleSort,InsertSort,SelectSort,QuickSort,ShellSort,HeapSort};
typedef int DataType[MAXSIZE+2];
static int Mix[]={ 0,1,4,16,64,128,256,512,4096};
DataType data;
DataType data2;
long com_times,shift_times;
int size;//

void BeforeSort()
{
    com_times=shift_times=0;
}

int shift(int i,int j)
{
    if(i<0||j<0||i>=size||j>=size)
        return FALSE;
    data[j]=data[i];
    shift_times++;
}
int swap(int i,int j)
 {
    if(i<0||j<0||i>=size||j>=size)
        return FALSE;
    int temp;
    temp=data[i];
    data[i]=data[j];
    data[j]=temp;
    shift_times+=3;
 }
 
int IsSmall(int i,int j)
 {
    if(i<0||j<0||i>=size||j>=size)
        return FALSE;
    com_times++;
    if(data[i]<data[j])
        return TRUE;
 }
void Initlist(int n);
{
    int i;
    for(i=1;i<=n;i++)
        data[i]=i;
    com_times=shift_times=0;
    size=n;
}

void RandomizeList(int d,int isInverse)
{
    if(!isInverse)
        InverseOrder();
    //打乱这些数
    int i;
    for(i=1;i<=Mix[d];i++)
        swap(random(size)+1,random(size)+1);
    //把这些数储存在data2中,以备下次排序的调用
    for(i=0;i<size;i++)
        data2[i]=data[i];
}


void RecallList()//还原数据,作下次排序
{
    int i;
    for(i=1;i<=size;i++)
        data[i]=data2[i];
}
    
void InverseOrder()
{
    int i;
    for(i=1;i<=size;i++)
        data[i]=size-i+1;
}

//insert sort
void InsertSort(long &c,long &s)
{
    int i,j;
    BeforeSort();//reset the com_times and the shift_times
    for(i=2;i<=size;i++)
    {
        data[0]=data[i];
        j=i-1;
        while(IsSmall(0,j))//find the insert position
        {
            shift(j,j+1);
            j--;
        }
        shift(0,j);
    }
    c=com_times;
    s=shift_times;
}
//shell sort
void ShellSort(long &c,long &s)
{
    BeforeSort();
    int i,h,j;
    for(h=size/2;h>0;h=(h-1)/2)// 对每个增量序列进行排序 h=(h+1)/2
    {
        for(i=h+1;i<=size;i++)//对各个子序列进行排序
        {
            shift(i,0);
            j=i-h;
            while(j>0&&IsSmall(0,j))//find the insert position
            {
                shift(j,j+h);
                j=j-h;
            }
            shift(0,j);
        }//for i
    }//for h
    c=com_times;
    s=shift_times;
}
// bubble sort
void BubbleSort(long &c,long &s)
{
    int i,j;
    BeforeSort();
    for(i=1;i<size;i++)
        for(j=size;j>i;j--)
            if(IsSmall(j,j-1))
                swap(j,j-1);
    c=com_times;
    s=shift_times;
}

//select sort
int SelectMinKey(int n)
{
    int i,j;
    j=n;
    for(i=n+1;i<=size;i++)
        if(IsSmall(i,j))
            j=i;
    return j;
}
void SelectSort(long &c,long &s)
{
    BeforeSort();
    int i,j;
    for(i=1;i<size;i++)
    {
        j=SelectMinKey(i);//find the smallest key in the sequeue between i and size
        if(j!=i)
            swap(i,j);
    }
    c=com_times;
    s=shift_times;
}//end select sort

//quick sort
void QSort(int high,int low)
{
    data[0]=data[low];
    int i,j;
    i=low;
    j=high;
    while(i < j)
    {
        while(i < j && IsSmall(0,j))
            j--;
        shift(j,i);
        while(i<j && IsSmall(i,0))
            i++;
        shift(i,j);
    }
    shift(0,i);
    QSort(low,i-1);
    QSort(i+1,high);
}

void QuickSort(long &c,long &s)
{
    BeforeSort();
    QSort(1,size);
    c=com_times;
    s=shift_times;
}// end  quick sort
    
// heap sort
void HeapAdjust(int s,int m)
{
    int temp,j;
    temp=data[s];
    for(j=2*s;j<=m;j=j*2)//find the position of temp  
    {
        if(j<m && IsSmall(j,j+1))
            j++;
        if(IsSmall(j,s))//do not need to adjust any maore
            break;
        shift(j,s);
        s=j;//store the possible of temp
    }//for j
    data[s]=temp;
}

void HeapSort(long &c,long &s)
{
    BeforeSort();
    int i;
    for(i=size/2;i>0;i--)// build the heap
        HeapAdjust(i,size);
    for(i=size;i>1;i--)//adjust the heap after sorting
    {
        swap(i,1);
        HeapAdjust(1,i-1);
    }//for i
    c=com_times;
    s=shift_times;
}

#endif

荀子《劝学》:“不积跬步,无以至千里;不积小流,无以成江海.”
2008-06-27 13:18
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
收藏
得分:0 
主程序在下面:
#include<stdio.h>
#include"OAList.h"
#include<conio.h>
#define max_group 18
#define min_group 8

//define function
void ReadCommand(char &c);
void Interept(char c);
int group;
extern void RandomizeList(int d,int isInverse);
extern void RecallList();
extern void InitList(int n);
int main()
{
    printf("**** 1.SortTest  2.Size(100 - 1000) 3.Group(8 - 18) q.Quit *****\n");
    printf("Command:\n");
    char cmd;
    do
    {
        //char cmd;
        ReadCommand(cmd);//read the command
        Interept(cmd);
    }while(cmd!='q'||cmd!='Q');
    return 0;
}//main

void ReadCommand(char &c)
{
    
    printf("if you want to compare each sort Algorithms ,first input 1,then 2,at last input 3;\
            if you want to quit,just input 'Q' or 'q';\n");
    do
    {
        c=getchar();
    }while(c!='1'&&c!='2'&&c!='3'&&c!='q'&&c!='Q');
    return;
}

void Interept(char c)
{
    switch(c)
    {
    case '1':
        //
        int i,j;
        int c,s;//the compare times and the shift times
        for(i=0;i<group;i++)
        {
            if(i<group/2)
                RandomizeList(i,TRUE);//对正序表作第I级打乱
            else
                RandomizeList(group-i-1,FALSE);//对逆序表作第I级打乱
            for(j=0;j<SORTNUM;j++)
            {
                if(j!=0)
                    RecallList();
                (*Sort[j])(c,s);/*test the j-th sort algorithm   error: `Sort' undeclared (first use this function)
                 error: (Each undeclared identifier is reported only once for each function it appears in.)*/
                //shown in output
                GotoXY(6+(j-1)*i,i+7);
                printf("%6ld",c);
                GotoXY(44+(j-1)*i,i+7);// does this function exist ?
                printf("%6ld",s);
            }//for j
        }//for i
        break;
    case '2':
        int n;//local or global ?
        printf("Size = ");
        scanf("%d",&n);
        if(n<100)
            n=100;
        if(n>1000)
            n=1000;
        printf("%d\t",n);
        InitList(n);
        break;
    case '3':
        //int group;//local or global ?
        printf("Group = ");
        scanf(" %d",&group);
        if(group < min_group)
            group=min_group;
        if(group > max_group)
            group=max_group;
        printf("%d\t",group);
        break;
    default:
        break;
    }//switch
}//interept

荀子《劝学》:“不积跬步,无以至千里;不积小流,无以成江海.”
2008-06-27 13:19
界水乘风
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-27 13:21
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
收藏
得分:0 
错误:
--------------------Configuration: adf - Win32 Debug--------------------
Compiling...
MainDemo.cpp
e:\files\adf\oalist.h(11) : error C2065: 'BubbleSort' : undeclared identifier
e:\files\adf\oalist.h(11) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(long,long)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\files\adf\oalist.h(11) : error C2065: 'InsertSort' : undeclared identifier
e:\files\adf\oalist.h(11) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(long,long)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\files\adf\oalist.h(11) : error C2065: 'SelectSort' : undeclared identifier
e:\files\adf\oalist.h(11) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(long,long)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\files\adf\oalist.h(11) : error C2065: 'QuickSort' : undeclared identifier
e:\files\adf\oalist.h(11) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(long,long)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\files\adf\oalist.h(11) : error C2065: 'ShellSort' : undeclared identifier
e:\files\adf\oalist.h(11) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(long,long)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\files\adf\oalist.h(11) : error C2065: 'HeapSort' : undeclared identifier
e:\files\adf\oalist.h(11) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(long,long)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
e:\files\adf\oalist.h(51) : error C2447: missing function header (old-style formal list?)
e:\files\adf\oalist.h(62) : error C2065: 'InverseOrder' : undeclared identifier
e:\files\adf\oalist.h(66) : error C2065: 'random' : undeclared identifier
e:\files\adf\oalist.h(81) : error C2373: 'InverseOrder' : redefinition; different type modifiers
e:\files\adf\oalist.h(89) : error C2373: 'InsertSort' : redefinition; different type modifiers
e:\files\adf\oalist.h(108) : error C2373: 'ShellSort' : redefinition; different type modifiers
e:\files\adf\oalist.h(130) : error C2373: 'BubbleSort' : redefinition; different type modifiers
e:\files\adf\oalist.h(152) : error C2373: 'SelectSort' : redefinition; different type modifiers
e:\files\adf\oalist.h(187) : error C2373: 'QuickSort' : redefinition; different type modifiers
e:\files\adf\oalist.h(212) : error C2373: 'HeapSort' : redefinition; different type modifiers
e:\files\adf\maindemo.cpp(61) : error C2065: 'GotoXY' : undeclared identifier
Error executing cl.exe.

adf.exe - 23 error(s), 0 warning(s)

荀子《劝学》:“不积跬步,无以至千里;不积小流,无以成江海.”
2008-06-27 13:21
onlyonegod
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2008-6-25
收藏
得分:0 
[bo][un]永夜的极光[/un] 在 2008-6-27 13:11 的发言:[/bo]

我觉得应该是这样调用
(Sort[j])(c,s);
你试试看



我觉得用函数名调用和用函数地址(函数指针)是都可以。
因为调用函数用函数名,他首先是根据函数名找到这个函数的入口地址,而函数地址(函数指针)就省略了这个找地址的一步了,效率能好那么一点点,呵呵
2008-06-27 13:38
界水乘风
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-27 13:44
快速回复:函数指针
数据加载中...
 
   



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

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