Thread: typedef issue

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    70

    typedef issue

    Code:
    template<typename Type>
    class GetTypeRegister
    {
    public:
    	typedef Type::InnerClass type;//Errors on this line
    };
    int main()
    {
    }
    I was wondering why this doesn't compile. This is a simplified example of something that would be very useful to me in some template work I'm doing. I wouldn't think this template would be generated at all as I never use it, and so should not generate errors. Perhaps typedef's work in a different fashion than I expect.

    It generates the following errors and warnings (file paths cut for brevity).
    Code:
     
    warning C4346: 'Type::InnerClass' : dependent name is not a type
       prefix with 'typename' to indicate a type
       see reference to class template instantiation 'GetTypeRegister<Type>' being compiled
    error C2146: syntax error : missing ';' before identifier 'type'
    warning C4517: access-declarations are deprecated; member using-declarations provide a better alternative
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    How does it know that InnerClass is a nested type and not some variable? Add typename like it says and perhaps it will work.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    typedef typename Type::InnerClass type;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Long story short, when working with anything that is a member of a template type, you must add typename before it (if it's a type).
    In your example, since Type is a template type to some class or something and InnerClass is a type, add typename.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Thank you, I'm still figuring out the subtleties of templates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM