Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在数组函数指针中调用void_C++_Arrays - Fatal编程技术网

C++ 在数组函数指针中调用void

C++ 在数组函数指针中调用void,c++,arrays,C++,Arrays,我已经编写了一个数组函数来在外部绘制openGL代码,而无需在每次创建新函数时在Windows的主循环中添加该函数 它工作正常,但当我将它指向另一个类中的公共void函数时,它表示“必须调用对非静态成员函数的引用” 代码: 绘图功能: typedef void (*function_ptr)(int); function_ptr *draw = (function_ptr*)malloc(sizeof(function_ptr)); 有没有办法称呼它 谢谢您遇到的问题如下: void (*f

我已经编写了一个数组函数来在外部绘制openGL代码,而无需在每次创建新函数时在Windows的主循环中添加该函数

它工作正常,但当我将它指向另一个类中的公共void函数时,它表示“必须调用对非静态成员函数的引用”

代码:

绘图功能:

typedef void (*function_ptr)(int);

function_ptr *draw = (function_ptr*)malloc(sizeof(function_ptr));
有没有办法称呼它


谢谢

您遇到的问题如下:

void (*function_ptr)(int);
是指向一个函数的指针,该函数的一个参数类型为
int

如果在这样的类中有一个方法:

class exampleClass
{
public:
    void example(int a);
};
void exampleClass@example(exampleClass *this, int a);

C++编译器将在内部生成一个隐式<代码> < <代码> >的函数,就像这样:

class exampleClass
{
public:
    void example(int a);
};
void exampleClass@example(exampleClass *this, int a);

(内部多数C++编译器使用这些字符的名称中的非法字符,如代码> @ /代码> -但是这里没有关系。< /P> 因此,通常不能将类方法分配给此类函数指针。您可以将

(exampleClass*,int)
类型的函数分配给
(int)
类型的函数指针

为了避免这种情况,编译器通常不允许您在函数指针中存储类方法

“At all”表示编译器也不允许您使用正确内部类型的函数指针:

void (*function_ptr_2)(exampleClass *, int);
exampleClass y;

/* Not supported by the compiler although it is
 * theoretically possible: */
function_ptr_2 x1 = exampleClass::example; 
function_ptr_2 x2 = y.example; 

遗憾的是,我的C++知识不够好,所以我不能告诉你Angew的解决方案(<代码> STD::函数< /C>)是如何工作的。

< P>你的问题是:
void (*function_ptr)(int);
是指向一个函数的指针,该函数的一个参数类型为
int

如果在这样的类中有一个方法:

class exampleClass
{
public:
    void example(int a);
};
void exampleClass@example(exampleClass *this, int a);

C++编译器将在内部生成一个隐式<代码> < <代码> >的函数,就像这样:

class exampleClass
{
public:
    void example(int a);
};
void exampleClass@example(exampleClass *this, int a);

(内部多数C++编译器使用这些字符的名称中的非法字符,如代码> @ /代码> -但是这里没有关系。< /P> 因此,通常不能将类方法分配给此类函数指针。您可以将

(exampleClass*,int)
类型的函数分配给
(int)
类型的函数指针

为了避免这种情况,编译器通常不允许您在函数指针中存储类方法

“At all”表示编译器也不允许您使用正确内部类型的函数指针:

void (*function_ptr_2)(exampleClass *, int);
exampleClass y;

/* Not supported by the compiler although it is
 * theoretically possible: */
function_ptr_2 x1 = exampleClass::example; 
function_ptr_2 x2 = y.example; 

不幸的是,我的C++知识不够好,所以我不能告诉你Angew的解决方案( STD::函数)是如何工作的。

查看。你可以张贴“窗口”类的定义吗?特别是“Digy”数组的定义。请看“窗口”类的定义,特别是“DRAW”数组的定义。