Thread: Problem with templates and typedefs

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109

    Problem with templates and typedefs

    I'm trying to write a class with a member fuction that returns an object of a type that's defined in one of the parameters.
    Something like this:

    Code:
    class J
    {
            public:
                    template<typename T>
                    typename T::name op(const T& o)
                    {
                            return T::name();
                    }
    };
    T in this case would be an object of this class:

    Code:
    template<typename T>
    class TI
    {
            public:
                    typedef T name;
    };
    with any random class used as the template parameter of TI
    I'm using this:

    Code:
    class I
    {
    };
    when I try to use it like this

    Code:
    #include "ideas2.h" // that's where the class definitions are
    
    int main()
    {
            /* other stuff I'm trying (currently commented out) removed */
    
            J j1;
    
            TI<I> ti1;
    
            j1.op(ti1);
    
            return 0;
    }
    gcc gives the following errors:

    Code:
    ideas2.h: In member function 'typename T::name J::op(const T&) [with T = TI<I>]':
    test.cpp:36:   instantiated from here
    ideas2.h:20: error: dependent-name 'T::name' is parsed as a non-type, but instantiation yields a type
    ideas2.h:20: note: say 'typename T::name' if a type is meant
    gcc says to use typename, but I'm already using it and if I remove it it says this instead:

    Code:
    ideas2.h:18: error: expected constructor, destructor, or type conversion before 'op'
    test.cpp: In function 'int main()':
    test.cpp:36: error: 'class J' has no member named 'op'
    Any ideas?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You can't assume anything about a generic template parameter, so T::name is illegal where T is a generic typename.

    "name" is inside class TI, so use
    Code:
    typename TI<T>::name
    [EDIT]also, looking at this line...
    Code:
    return T::name();
    'name' is a typename.. not an object... nor a function.. I'm not entirely clear which object you're trying to return from this function. Are you attempting to create a new object of type 'name' ?


    One suggestion commonly given to people trying to create templated classes from scratch is to create your class structure(s) first, without templates (use some built-in type like a char or int for testing) ... then once the bugs are ironed out, and it works correctly for that single type, put the templates in.. because you will undoubtedly have additional problems involving templates.
    Last edited by Bench82; 03-27-2006 at 11:38 AM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Well...

    this compiles fine

    Code:
    class J
    {
            public:
                    template<typename T>
                    void op(const T& o)
                    {
                            typename T::name var;
                    }
    };
    in fact

    Code:
    class J
    {
            public:
                    template<typename T>
                    typename T::name op(const T& o)
                    {
                            typename T::name var;
                            return var;
                    }
    };
    works fine.

    By the way, I'm stupid, stupid stupid.
    I kept on looking for errors in the function signature when the error was in the original return statement
    Last edited by sigfriedmcwild; 03-27-2006 at 11:44 AM.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Bench82
    [EDIT]also, looking at this line...
    Code:
    return T::name();
    'name' is a typename.. not an object... nor a function.. I'm not entirely clear which object you're trying to return from this function. Are you attempting to create a new object of type 'name' ?
    Yeah that's what I'm trying to do return a new object of type T::name
    Since normally you can do
    Code:
    ...
    return some_type();
    I thought it would work as well. It seems there's some issues when that gets mixed with the typename keyword

    This seems to work though
    Code:
    typename T::name p = new typename T::name();
    Oh well, thanks forthe help
    Last edited by sigfriedmcwild; 03-27-2006 at 11:56 AM.

Popular pages Recent additions subscribe to a feed