I want to do the following :-
#include <iostream>
template <typename I>
class A {
public:
I member_;
void f() {}
void g() {}
typedef void (A::*fptr) ();
static const fptr arr[];
};
template <typename I>
A<I>::fptr A<I>::arr[] = {
&A<I>::f,
&A<I>::g
};
How do I do this?
I get the following errors :-
g++ memeber_func_ptr_array.cpp
memeber_func_ptr_array.cpp:14:1: error: need ‘typename’ before ‘A<I>::fptr’ because ‘A<I>’ is a dependent scope
memeber_func_ptr_array.cpp:17:2: error: expected unqualified-id before ‘;’ token
Two things.
fptris a dependent type so you needtypename:And as jrok noted in the comments, your declaration is
constso the definition must beconstas well.Client code (files that just include the header) needs to know how big the array is so you need the actual size of the array in the declaration:
You can only use the automatic deduction of the size of the array when the array is declared and initialised in the same place.