2011-05-10 37 views
2

所以,我定義爲一個函數指針:C++:我用什麼類型來定義這張地圖?

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

而我試圖讓地圖指針的函數名:

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

但我得到這個錯誤:

src/main.h:24: error: ISO C++ forbids declaration of ‘fptr_t’ with no type

其他代碼:

main.h

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

不是你的問題的答案,但是...如果你的編譯器支持一些C++ 0x功能,我建議使用std :: function通過typedefs函數指針。他們會更容易閱讀,並且更加靈活。 – luke 2011-05-10 19:54:06

回答

0

你的函數指針的類型定義是:

typedef unsigned int (*fptr_t)(unsigned int) 

...那麼你就可以宣佈你喜歡的地圖這個:

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

你記得的typedef函數指針?

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

我相信這就是正確的語法

+2

'unsigned static int'是無意義的。 – ildjarn 2011-05-10 19:27:16

+0

這是真的,我只是複製他的功能足跡 – 2011-05-10 19:28:44

+1

它遵循格式'typedef return_type(* name)(arg1_type,..,argN_type)'所以你是正確的,我相信。 – Chad 2011-05-10 19:28:50

1

你「main.h」代碼不給函數指針的typedef返回類型。這個工作對我來說:

#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; 
} 
1

你已經錯過了返回類型:

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

你的typedef的函數指針缺少返回類型:

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

以上是一個指針,指向返回unsigned int並且具有unsigned int *作爲參數的功能的typedef

相關問題