hello
i'm trying to implement some functionality whereby an algorithm in a
base template class relies on a function pointer supplied by a derived
template class. the types are only specified by the client (caller) of
the derived class.
i got it working under visual studio 2003/2005 but gcc 4.1.0
compilation fails. here's the smallest subset of the code that shows
the error:
template <class T>
class Foo {
protected:
typedef bool (*FuncPtr)(const T&);
virtual FuncPtr *getFuncPtr() = 0;
private:
// algorithm which invokes getFuncPtr()
// and does stuff with the pointer returned
// ...
};
// one example of a family of functions
// suitable for use in the algorithm:
template <class T>
bool func1(const T &x) { return x 1; }
// one example of a family of derived classes
// which implement getFuncPtr() plus other behavior:
template <class T>
class Bar : public Foo<T{
FuncPtr *getFuncPtr() {
static FuncPtr f = func1<T>;
return &f;
}
// other stuff...
};
int main() {
Bar<intb;
return 0;
}
here's the error returned by gcc:
[erik@laptop2 cpp_test]$ make
g++ -o test1 test1.cpp
test1.cpp:23: error: ISO C++ forbids declaration of
'FuncPtr' with no type
test1.cpp:23: error: expected ';' before '*' token
test1.cpp:28: error: expected `;' before '}' token
test1.cpp: In function 'int main()':
test1.cpp:31: error: cannot declare variable 'b' to
be of abstract type
'Bar<int>'
test1.cpp:22: note: because the following virtual
functions are pure within
'Bar<int>':
test1.cpp:6: note: bool (** Foo<T>::getFuncPtr())
(const T&) [with T = int]
make: *** [test1] Error 1
[erik@laptop2 cpp_test]$
what's wrong with the code syntactically?
is there a better way to do what i'm trying to do?
thanks,
eric