[C++] Access typedef in templated class

Started by
6 comments, last by BTownTKD 14 years, 11 months ago
Hi, the following code works:
    class Function
    {
        typedef void (*FUNCTION)();

    public:
        FUNCTION function() const;
    
    private:
        FUNCTION _function;
    }

    Function::FUNCTION Function::function() const
    {
        return _function;
    }
But, the following doesn't! What is wrong? How can I fix this?
    template<class T>
    class Function
    {
        typedef void (*FUNCTION)(T *);

    public:
        FUNCTION function() const;
    
    private:
        FUNCTION _function;
    }

    template<class T>
    Function::FUNCTION Function<T>::function() const // neither if I use Function<T>::FUNCTION ...
    {
        return _function;
    }
Thanks
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
typename Function<T>::FUNCTION
Thanks, I never see when to use typenames...the errors the compiler gives aren't very clear that a typename keyword is missing :P
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Maybe you should try a different compiler. With MSVC 2008 you get:
1>.\main.cpp(16) : warning C4346: 'Function<T>::FUNCTION' : dependent name is not a type1>        prefix with 'typename' to indicate a type

if you try compiling your code without typename. Seems pretty clear to me.
And what about:

    template<class T>    class Function    {        typedef void (*FUNCTION)(T *);    public:        const FUNCTION &function() const;        private:        FUNCTION _function;    }    template<class T>    const Function<T>::FUNCTION &Function<T>::function() const    {        return _function;    }


Quote:Original post by SiCrane
Maybe you should try a different compiler. With MSVC 2008 you get:
1>.\main.cpp(16) : warning C4346: 'Function<T>::FUNCTION' : dependent name is not a type1>        prefix with 'typename' to indicate a type

if you try compiling your code without typename. Seems pretty clear to me.


Yes, I use GCC. It's error messages are often rather vague I find.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
And what about:

And what about it?
Hmm, been trying several combinations, but the one posted gives me:

error: expected initializer before '&' token

Not sure where, and if, to place the typename too.

EDIT:
Okay, got it almost working, let me figure this out ;-) (it's now only complaining for a class with 2 template items)...

EDIT2:
Got it! Thanks SiCrane!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
This part made me giggle:

Quote:
Function<T>::FUNCTION &Function<T>::function()


It's pretty much the most readable code ever.

Also, when I look at the word "Function" that many times, my mind starts trying to trick me into thinking that it's spelled wrong.
Deep Blue Wave - Brian's Dev Blog.

This topic is closed to new replies.

Advertisement