1
class Emp
{
    int no;
    char name[50];
public:
    void get_data();
} e;

void func_get();

enum ITEMTYPE
{
    MI_PARENT, MI_ACTION
};

struct MENUITEM {
    enum ITEMTYPE type;
    char *name;
    char *helpstring;
    void *p;
};

  // 1
struct MENUITEM get_data = {
    MI_ACTION,"Get","Enter to Get", e.get_data() }; // calling get_data() of type void by using abject e

// 2
struct MENUITEM root_draw = {
  MI_PARENT, "Options", "Enter to list the Options Menu", (void*)func_get /* calling   Global function which have return type void */
};
 struct MENUITEM *draw_list[] = {
  &get_data
 };

我在我的程序中使用上面的代码,当我调用全局函数(case//2)时,程序正在正确执行。但是当我尝试使用类的对象调用函数时(case //1)它显示错误“值无效类型的不允许”。谁能告诉我什么是解决方案。无论如何我都想调用我的类函数。提前致谢。

4

2 回答 2

2

I suppose the fourth member of MENUITEM is supposed to be a pointer to a callback function. (void*)func_get is the address of the function func_get cast to void*. Similarly, you can get the address of &Emp::get_data, but bare in mind the function belongs to the class, not the object, so you cannot say &e.get_data. Also, the function get_data being a non-static member of the class has a hidden parameter, that is pointer this (Emp* this). As a result, the signatures of func_get and get_data are not identical and calling get_data from outside the class through a function pointer should require you to pass the pointer to an object.

于 2013-10-08T20:42:45.210 回答
0

检查语法

(void*)func_get - this is not function call. 
于 2013-10-08T20:34:33.413 回答