Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[C++] Access typedef in templated class

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Thanks, I never see when to use typenames...the errors the compiler gives aren't very clear that a typename keyword is missing :P

Share this post


Link to post
Share on other sites
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 type
1> prefix with 'typename' to indicate a type

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

Share this post


Link to post
Share on other sites
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 type
1> 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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!