Reputation: 3528
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
Upvotes: 2
Views: 119
Reputation: 75130
Two things.
fptr
is a dependent type so you need typename
:
template <typename I>
const typename A<I>::fptr A<I>::arr[2] = { // also note the 2 and the const
&A<I>::f,
&A<I>::g
};
And as jrok noted in the comments, your declaration is const
so the definition must be const
as 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:
static const fptr arr[2]; // include size
You can only use the automatic deduction of the size of the array when the array is declared and initialised in the same place.
Upvotes: 4
Reputation: 20616
You need to add const typename
before A<I>::fptr
. The typename
is there to tell the compiler that fptr
is a type within A<I>
.
You might want to look at C++ Templates: The Complete Guide by Vandevoorde and Josuttis for more information.
Upvotes: 2
Reputation: 361254
Use typename
as:
template <typename I>
typename A<I>::fptr A<I>::arr[] = { &A<I>::f, &A<I>::g };
//^^^^^^^^note this
It is because fptr
is a dependent type.
Upvotes: 0