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

。函数指针数组。不懂错误的意思

kun内阁双狂 发布于 2013-12-10 21:59, 1067 次点击
#include<iostream>
using namespace std;
void square(float &x, float &y)
{
    x=x*x;
    y=y*y;
}
void cube(float &x, float &y)
{
    x=x*x*x;
    y=y*y*y;
}
void print(float &x, float &y)
{
    cout<<"长:"<<x<<"\t"<<"宽:"<<y<<endl;
}
void swap(float &x, float &y)
{
    float z;
    z=x;
    x=y;
    y=z;
}
int main(void)
{
    float a=2,b=3;
    char choice='0';
    int i;
    void (*p[5])(float &, float &);
    for (i=0; i<5; i++)
    {
        bool quit = false;
        cout<<"(0)退出,(1)平方,(2)立方,(3)交换x和y的值:";
        cin>>choice;
        switch (choice)
        {
        case '0': quit=true;    break;
        case '1': p[i]=square;    break;
        case '2': p[i]=cube;    break;
        case '3': p[i]=swap;    break;                //错误指向的地方
        default:p[i]=0;
        }
        if (quit)break;
        if (p[i]==0)
        {
            cout<<"请输入0到3之间的数字:";
            i=i-1;
            continue;
        }
        cout<<"第"<<i<<"次执行,到第5次结束\n";
        cout<<"初始值\n";
        print(a,b);
        cout<<"现在调用函数指针数组p["<<i<<"]所指向的函数...\n";
        p[i](a,b);
        cout<<"运算后\n";
        print(a,b);
    }
    return 0;
}
/*
——————————————————————————————————————
E:\c++\108函数指针数组\01.cpp(40) : error C2563: mismatch in formal parameter list
E:\c++\108函数指针数组\01.cpp(40) : error C2568: '=' : unable to resolve function overload
        could be 'void __cdecl swap(float &,float &)'
        E:\c++\108函数指针数组\01.cpp(17) : see declaration of 'swap'
        or       'void __cdecl std::swap(float &,float &)'
        d:\vc98\include\xutility(99) : see declaration of 'swap'
        or       'void __cdecl std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::swap(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std:
:char_traits<char>,class std::allocator<char> > &)'
        d:\vc98\include\xstring(378) : see declaration of 'swap'
        or       'void __cdecl std::swap(_Ty &,_Ty &)'
        d:\vc98\include\xutility(99) : see declaration of 'swap'
Error executing cl.exe.

01.obj - 2 error(s), 0 warning(s)
——————————————————————————————————————
*/
12 回复
#2
peach54602013-12-11 08:13
函数重名了
#3
欣飞飞2013-12-11 14:06
print本来就是输入swap函数本来就是交换,没必要在写个函数直接可以用的
#4
欣飞飞2013-12-11 14:07
printf是输入
#5
jiangcw0072013-12-11 16:25
只有本站会员才能查看附件,请 登录
#6
jiangcw0072013-12-12 15:43
太有意思了!我刚刚又思考了一下你的程序,发现无法绕过库函数,然后尝试用类来绕过去,发现可行,于是使用成员函数指针实现具体步骤,不过写完了,总觉得有一些多余的地方,总是发现不了,发出来大家给看看;

程序代码:
#include <iostream>
using namespace std;
class swp
{   
public:
    void swap(int &a,int &b)
    {
        int c;
        c=a;a=b;b=c;
        cout<<endl<<"swp::"<<endl;
    }
};
void swap(int &a,int &b)
{
    int c;
    c=a;a=b;b=c;
    cout<<endl<<"swap"<<endl;
}
int main()
{
    int a,b;
        a=1;
        b=2;
    swp ss;
    {
        typedef void (swp::*p)(int&,int&);
        p pclass = ss.swap;
        (ss.*pclass)(a,b);
        cout<<"指针调用:a="<<a<<" b="<<b<<endl;
        
    }
    {
        swap(a,b);
        cout<<"函数调用:a="<<a<<" b="<<b<<endl;
    }

    return 0;
}
#7
kun内阁双狂2013-12-12 20:52
回复 5楼 jiangcw007
/*
    根据5楼的回复我重新梳理了该程序,希望对各楼也有所收益:我学c++不是很久,对库的认识不够,不知道swap函数属于在库中已有定义,
    以至于读不懂错误的信息,我是把void型函数指针改为int型函数指针,在对应出也作出相应的改动,程序如下。5楼的第一种改法很简单,
    看了自然会懂。只是不知道使用这两种的利弊。。在这里顺便说一下6楼的程序,刚才始看很模糊,有几个代码以前没有看过,动了一下脑筋,
    再整理这些代码,还是可以明白他的意思的。谢谢各楼!谢谢!
*/
#include<iostream>
using namespace std;
int square(float &x, float &y)
{
    x=x*x;
    y=y*y;
    return x,y;
}
int cube(float &x, float &y)
{
    x=x*x*x;
    y=y*y*y;
    return x,y;
}
void print(float &x, float &y)
{
    cout<<"长:"<<x<<"\t"<<"宽:"<<y<<endl;
}
int swap(float &x, float &y)
{
    float z;
    z=x;
    x=y;
    y=z;
    return x,y;
}
int main(void)
{
    float a=2,b=3;
    char choice='0';
    int i;
    int (*p[5])(float &, float &);
    for (i=0; i<5; i++)
    {
        bool quit = false;
        cout<<"(0)退出,(1)平方,(2)立方,(3)交换x和y的值:";
        cin>>choice;
        switch (choice)
        {
        case '0': quit=true;    break;
        case '1': p[i]=square;    break;
        case '2': p[i]=cube;    break;
        case '3': p[i]=swap;    break;                //错误指向的地方
        default:p[i]=0;
        }
        if (quit)break;
        if (p[i]==0)
        {
            cout<<"请输入0到3之间的数字:";
            i=i-1;
            continue;
        }
        cout<<"第"<<i<<"次执行,到第5次结束\n";
        cout<<"初始值\n";
        print(a,b);
        cout<<"现在调用函数指针数组p["<<i<<"]所指向的函数...\n";
        p[i](a,b);
        cout<<"运算后\n";
        print(a,b);
    }
    return 0;
}
#8
IT男year2013-12-12 21:03
void (*p[5])(float &, float &);大神这句什么意思呀?帮忙解释一下,我好想没有见过!

[ 本帖最后由 IT男year 于 2013-12-12 22:02 编辑 ]
#9
peach54602013-12-13 09:44
回复 6楼 jiangcw007
你觉得命名空间是干嘛的...
你不要用using namespace
自然就不会重...

废那心思,还拿个类包着干嘛...
简单问题复杂化...
#10
peach54602013-12-13 09:45
回复 8楼 IT男year
C语言定义函数指针的一种方式
#11
jiangcw0072013-12-13 21:54
回复 7楼 kun内阁双狂
你好,只是感觉你开始的回复,对于新手来说,人家会困扰很多时间,函数指针是C++里面一个比较复杂的部分,算是指针当中的精华,而你光说一句函数重名,你觉得人家听得懂吗》?我觉得只要有专研精神,只要自己肯学,一个好的指点可以帮助他省很多时间,你后面说的using namespace std; 也是一种方法,我承认确实疏忽了这种方法,但是我更倾向于我的思路,最简单不过修改函数模板的匹配规则,复杂点的使用类包起来用一下成员函数指针,这些对于我们掌握这些新的知识点都是很好的过程,特别是在新手的时候,增长点见识总是没错的;
#12
peach54602013-12-13 23:25
编译提示的意思是...没有当前的函数重载,而且编译信息的提示很清楚的指明了重重载函数的位置...提示在系统头文件里面...
我回答重名了,原意是想让他自己去查编译信息,发现错误...
我没觉得我回答有误导之嫌...

而你的结局方案不失为一种办法...
但是我更加认为对于初学者,反而去规避定义函数与系统函数重名,比用类去包更简单,而且容易理解...
这就是我所谓的简单问题复杂化...
你自己设想一下...下次在发生类似的编译错误...
他回想着用类再去包...但是这份代码不是这么好理解的,你觉得以楼主现在的水平,他不看你的样例写得出来么...
我更认为我告诉他重名了,他改个函数名岂不是更省事...
#13
chenwei4352013-12-27 14:02
路过。。。。
1