I'm trying to declare a template function pointer in C++.
template <class T>
class MyClass
{
public:
typedef const unsigned char* (T::*MyTemplatedEvent)(unsigned long &myParameter);
};
but for some reason I keep getting this error:
'T': must be a class or namespace when followed by '::'
Can somebody tell what I'm doing wrong?
the compiler should know that T is a class. It says so above the MyClass declaration...
With T::*MyTemplatedEvent
, you are expecting T
to be a class type, since only class types can have member pointers. This means that if you pass a non-class type, say int
or char*
, you get the indicated error, as there are no members and conversely no member pointers for those types.
the compiler should know that T is a class. It says so above the MyClass declaration...
Wrong. class T
is the same as typename T
in a template parameter, and only tells the compiler that T
is a placeholder for any type that is passed as a template argument later on. It does not restrict the type to be of class type.
I'm guessing it's because I try to create "MyClass" of template which is a pointer or primitive which the compiler know can't have and function pointer associated with them...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With