PDA

View Full Version : C++ Template Question




Neutral Gamer
Nov 10, 2010, 11:12 AM
Hi guys, I'm porting over some C++ code that I wrote for Windows where it works fine (Visual C++) but in Mac OS X I'm getting errors. Here's the problem:

Let's say I've got a simple base class as follows:


template <typename T>
struct BaseClass
{
typedef void (*HandlerType) (int);
};


And then a derived class as follows:


template <typename T>
struct DerivedClass : public BaseClass<T>
{
void Testground (HandlerType Handler)
{
}
};


When trying to compile I get an error that says "HandlerType has not been declared". I also tried using the fully qualified name for the HandlerType:


struct DerivedClass : public BaseClass<T>
{
void Testground (BaseClass<T>::HandlerType Handler)
{
}
};


But then I get the error: "struct BaseClass<T>::HandlerType is not a type". If I don't make the classes templates then it works fine. I'm sure there's a really simple solution to this problem that's staring me in the face but I'm new to Mac / GCC programming so any help would be much appreciated!

Cheers. :)



gnasher729
Nov 10, 2010, 12:18 PM
gcc is stricter and usually more correct than Microsoft compilers. So you don't have a Mac/gcc problem, you have a C++ problem.

HandlerType is not based on the template, that is most likely the cause of the problem. That and some bizarre rules about instantiating templated stuff that gcc implements closer to the C++ Standard than others.

AlmostThere
Nov 10, 2010, 02:14 PM
You need to qualify non-dependent types with typename (there is nothing to specify that HandlerType is actually a type, otherwise)


struct DerivedClass : public BaseClass<T>
{
void Testground (typename BaseClass<T>::HandlerType Handler)
{
}
};

Neutral Gamer
Nov 15, 2010, 09:09 PM
Thank you SO MUCH AlmostThere! Simply adding "typename" fixed all my problems - don't know how I would have figured that one out myself so maximum respect. ;)

Sorry for the late reply - had no internet for the last week!

Cheers. :)