C++:我使用什么类型来定义此地图

C++: What type do I use for the definition of this map?

本文关键字:定义 地图 类型 什么 C++      更新时间:2023-10-16

所以,我有一个函数指针定义为:

unsigned static int (*current_hash_function)(unsigned int);

我正在尝试制作指向函数名称的指针映射:

typedef std::map<fptr_t, std::string> function_map_t;

但是我收到此错误:

src/main.h:24:错误:ISO C++禁止声明无类型的‘fptr_t’

其他代码:

主.h

typedef (*fptr_t)(unsigned int*);
typedef std::map<fptr_t, std::string> function_map_t;
function_map_t fmap;

你的"main.h"代码没有为函数指针类型定义提供返回类型。这对我有用:

#include <map>
#include <string>
int main()
{
    typedef unsigned (*fptr_t)(unsigned);
    typedef std::map<fptr_t, std::string> function_map_t;
    function_map_t fmap;
}

您错过了返回类型:

typedef int (*fptr_t)(unsigned int*);

函数指针的类型定义是:

typedef unsigned int (*fptr_t)(unsigned int)

。然后,您可以像这样声明您的地图:

typedef std::map<fptr_t, std::string> function_map_t;

你记得对函数指针进行 typedef 吗?

typedef unsigned int (*fptr_t)(unsigned int);

我相信这是正确的语法

函数

指针的typedef缺少返回类型:

typedef unsigned int (*fptr_t)(unsigned int *);

以上是指向返回 unsigned int 并将unsigned int *作为参数的函数的指针的typedef