我有這樣的代碼定義結構(傳入是簡單的結構)C++在陣列結構體的成員函數指針表示編譯錯誤
#define FUNCS_ARRAY 3
struct func
{
void (AA::*f) (incoming *);
int arg_length;
};
func funcs[FUNCS_ARRAY];
然後在此類AA體我這樣定義指針數組:
funcs[0] = { &AA::func1, 4 };
funcs[1] = { &AA::func2, 10 };
funcs[2] = { &AA::func2, 4 };
當我嘗試經由所述陣列即時得到編譯錯誤調用的功能中的一個:
如果我這樣稱呼它(p是進入):
(*funcs[p->req]->*f)(p);
即時得到這樣的錯誤:
error: no match for ‘operator*’ in ‘*((AA*)this)->AA::funcs[((int)p->AA::incoming::req)]’
當我嘗試調用它是這樣的:
(funcs中[對 - > REQ] - > * F)(對);
即時得到:
error: ‘f’ was not declared in this scope
當我試試這個:
(funcs[p->req].f)(p);
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((AA*)this)->AA::funcs[((int)p->AA::incoming::req)].AA::func::f (...)’, e.g. ‘(... ->* ((AA*)this)->AA::funcs[((int)p->AA::incoming::req)].AA::func::f) (...)’
什麼是訪問函數指針在側結構的正確方法?
你可以使用['標準:: function'(http://en.cppreference.com/w/cpp/utility/functional/function)或[ 'boost :: function'](http://www.boost.org/libs/function/)來做所有的事情爲你包裝 – KillianDS 2012-07-21 14:35:31