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

【求教】请问大家:指向函数的指针作为一个类的成员,如果通过对象调用它,实现调用所指向的函数的目的?

jojo59516 发布于 2012-04-12 23:40, 545 次点击
问题RT,下面给出自己编写的程序(一部分)作为错误的例子,希望大神帮忙修改,最好能有解释,谢谢啦~~~~!!!!

#include <iostream>
#include <string>
class Equation
{
private:
    int answer,m;
    void (*mode[3])();                                  //定义了一个指向函数的指针数组
public:
    Equation();
    void setmode();
    void addition();
    void subtraction();
    void multiplication();
    int getanswer () const {return answer;}
    void *run () const {return mode[m];}                 //定义了一个指针类的函数,我希望通过这个指针函数来调用那个指针,从而调用所指向的函数
};
#endif
#include "Random.h"
#include "Equation.h"
#include <string>
#include <iostream>
using namespace std;
namespace
{    const string CORRECT_INFO[]={"Good job!","Correct,again!","You are so clever!","Wow,a talent!","You did it, Congratulations!"};
    const string WRONG_INFO[]={"Just carelessness,cheer up!","What a pity!","Sorry, you are fail."};
};
int main()
{    int score,ans,i,j;
    char whethergo;
    Equation equ;
    cout<<"Welcome to Guess!"<<endl;
    for (i=1;i<=10;i++)
    {    cout<<"Question "<<i<<endl;
        *equ.run();                                    //调用了那个指针类的函数,编译后报错
        for (j=0;j<3;j++)
        {    cin>>ans;
            if (ans==equ.getanswer())
            {    Random r(4);
                score+=10;
                cout<<"Right!"<<endl<<CORRECT_INFO[r.getr()]<<endl;
                break;
            }
            else
            {    Random r(2);
                cout<<"Wrong"<<endl<<WRONG_INFO[r.getr()]<<endl;
            }
        }
        if (j==3)
            cout<<"Sorry, you have no chances."<<endl;
        cout<<"Go ahead?(Y/N)";
        cin>>whethergo;
        if (whethergo=='N')
        {    cout<<"Your score is "<<score<<endl;
            break;
        }
    }
    if (i==11)
        cout<<"You have complete the game!"<<endl<<"Your score is "<<score<<endl;
    cout<<"Thank you for your support! Press any key to quit.";
    cin>>i;
    return 0;
}

--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
F:\study\VC++\guess\main.cpp(17) : error C2100: illegal indirection
执行 cl.exe 时出错.

main.obj - 1 error(s), 0 warning(s)

我的最终目的是在主程序中设法调用指向函数的指针*mode[i],通过它运行所指向的程序。至于那个指针类的函数(外部接口)*run()是我自己理解的,不知道这样的做法对不对。我是初学者,真心求教~!!谢谢~~~~~~!!
8 回复
#2
寒风中的细雨2012-04-13 00:09
(&ClassName::FunctionName)试这格式
#3
寒风中的细雨2012-04-13 01:09
程序代码:
#include <iostream>
using namespace std;

class CModel;
typedef void (CModel::*Func)();
typedef void (*StaFunc)();

class CModel
{
private:
    Func mode[3];
public:
    CModel(){
        mode[0]=&CModel::F1;
        mode[1]=&CModel::F2;
        mode[2]=&CModel::F3;
    };
    void F1(){cout << "F1()" << endl;};
    void F2(){cout << "F2()" << endl;};
    void F3(){cout << "F3()" << endl;};
    void Run(int i){(this->*mode[i])();}
    Func R(int i){return mode[i];};
    static void F(){cout << "///" << endl;}
};

int main()
{
    CModel m;

    m.Run(1);

    ((&m)->*(m.R(2)))();

    StaFunc f = &CModel::F;

    f();

    return 0;
}
#4
jojo595162012-04-13 13:05
回复 3楼 寒风中的细雨
感谢版主大人~~~~~写的太详细了好感动~~~~
#5
寒风中的细雨2012-04-13 14:19
回复 4楼 jojo59516
是你粘上来的代码没我没法领略大意   所以
#6
jojo595162012-04-14 00:08
回复 4楼 jojo59516
编译通过了,连接又有问题了T T继续求教啊。。。。谢谢谢谢谢谢
直接把程序代码传上来了。。。这个程序用来生成一个50以内的加减或9以内的乘法算式题,并让用户输入结果,有三次常识机会,一共10题每答对一道题10分。
新手写代码习惯可能不好。。。总之谢谢谢谢谢谢。。。

连接后错误:
--------------------Configuration: main - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Random::Random(int)" (??0Random@@QAE@H@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Equation::Equation(void)" (??0Equation@@QAE@XZ)
Debug/main.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
只有本站会员才能查看附件,请 登录
#7
寒风中的细雨2012-04-14 10:36
无法上传附件

xxxx.h 文件内的成员定义  全部提出来 写到xxxx.cpp中
程序代码:


//main.cpp
namespace std
{   
    const string CORRECT_INFO[]={"Good job!","Correct,again!","You are so clever!","Wow,a talent!","You did it, Congratulations!"};
    const string WRONG_INFO[]={"Just carelessness,cheer up!","What a pity!","Sorry, you are fail."};
}
using namespace std;

//Equation.cpp
void Equation::subtraction()
{   
    Random num1(50),num2(50);
    int sub1,sub2;
    sub1=num1.getr()>num2.getr()?num1.getr():num2.getr();
    sub2=num1.getr()+num2.getr()-sub1;
    cout<<sub1<<"-"<<sub2<<"=";
    answer=sub1-sub2;
}

//输出
Welcome to Guess!
Question 1
34-17=2
Wrong
Just carelessness,cheer up!


最后可以运行成功,  答错了可以运行  答对了就有问题啦  自己改改吧


#8
jojo595162012-04-16 01:05
回复 6楼 jojo59516
可是书上还说最好把类的定义写到头文件里呢,现在又运行不成功。。这是为什么呢??
#9
寒风中的细雨2012-04-16 09:04
回复 8楼 jojo59516
那是类的定义,  说的是成员函数的定义
1