C++ Template Question

Discussion in 'Mac Programming' started by Neutral Gamer, Nov 10, 2010.

  1. macrumors 6502a

    Neutral Gamer

    #1
    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:

    Code:
    template <typename T>
    struct BaseClass
    {
        typedef void (*HandlerType) (int);
    };
    
    And then a derived class as follows:

    Code:
    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:

    Code:
    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. :)
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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.
     
  3. macrumors 6502a

    #3
    You need to qualify non-dependent types with typename (there is nothing to specify that HandlerType is actually a type, otherwise)

    Code:
    struct DerivedClass : public BaseClass<T>
    {
        void Testground (typename BaseClass<T>::HandlerType Handler)
       {
       }
    };
    
     
  4. thread starter macrumors 6502a

    Neutral Gamer

    #4
    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. :)
     

Share This Page