小编Ves*_*del的帖子

C++中的一系列函数指针

我正在试图弄清楚如何创建一个指向类内部函数的指针数组.数组中的元素表示唯一函数的位置.

Tiles.h代码:

class tiles: public box
{
    public:
        void north2east(float trans_x, float trans_y);
        void north2west(float trans_x, float trans_y);
        void south2east(float trans_x, float trans_y);
        void south2west(float trans_x, float trans_y);
};
Run Code Online (Sandbox Code Playgroud)

Tiles.cpp代码:

void tiles::north2east(float trans_x, float trans_y); { }
void tiles::north2west(float trans_x, float trans_y); { }
void tiles::south2east(float trans_x, float trans_y); { }
void tiles::south2west(float trans_x, float trans_y); { }
Run Code Online (Sandbox Code Playgroud)

我听说你可以通过在Tiles.cpp文件中添加以下内容来实现:

typedef void (*FUNC_ARRAY) (float trans_x, float trans_y);
FUNC_ARRAY functions[] = {
    tiles::north2east,
    tiles::north2west,
    tiles::south2east,
    tiles::south2west
}
Run Code Online (Sandbox Code Playgroud)

但这给了我以下错误:

类型为"void(tiles ::*)(float trans_x,float trans_y)"的值不能用于初始化"FUNC_ARRAY"类型的实体 …

c++ arrays function

3
推荐指数
1
解决办法
113
查看次数

标签 统计

arrays ×1

c++ ×1

function ×1