Template class nested in template class

This is a discussion on Template class nested in template class within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a container class that has node-like objects. These need to be templates. I tried: Code: template ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    Template class nested in template class

    I'm trying to create a container class that has node-like objects. These need to be templates. I tried:

    Code:
    template <class T>
    class H {
     private:
      template <class T> class K;
      //...
    };
    
    template <class T>
    class H<T>::K {
     //...
    };
    But that doesn't work. I tried some random things in hopes that I'd get the combination right, but none of them worked.

    What am I doing wrong? How do I fix this?

    Thank you

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    You can't reuse "T" as a template parameter. How could the inner class possibly distinguish between the two? Name it something else.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    I didn't want it to. The types always have to be the same. I guess this is achieved by having two separate variables anyways, though. Yes?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by yahn View Post
    I didn't want it to. The types always have to be the same. I guess this is achieved by having two separate variables anyways, though. Yes?
    If the types have to be the same then the inner class does not need to be a template.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Oh, that makes sense. How about when you want to return the nested type?

    I have:
    Code:
    template<class T>
    class H {
     private:
      class K;
      // ...
    };
    
    Class K {
     // ...
     public:
      K *doSomething();
      // ...
    };
    
    template<class T>
    H<T>::K *H<T>::K::doSomething() {
     // ...
    };
    That gives the error : "dependent name is not a type prefix with 'typename' to indicate a type"

    Do you know what I'm doing wrong?

    Thank you

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    That looks wrong. If K is nested in H, then I would expect you to define it as:
    Code:
    template<class T>
    class H<T>::K {
    public:
        K* doSomething();
    };
    though personally I would just define it within the definition of H. Anyway, once that is fixed, the next problem would be that in H<T>::K, the nested name K depends on the template parameter T. As such, it could, in theory, be interpreted not as a type name, but as an object name, and the rules say that it should be interpreted as a non-type name by default. As such, you should change to:
    Code:
    template<class T>
    typename H<T>::K* H<T>::K::doSomething() {
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Sorry, I had properly declared the class, I just didn't write it done correctly when making the example. But, you're suggested works. Thank you

    Now, I'm not getting any errors about the class, but when trying to compile I am getting unresolved external symbols to the functions of the class. What does that mean?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by yahn
    Now, I'm not getting any errors about the class, but when trying to compile I am getting unresolved external symbols to the functions of the class. What does that mean?
    My guess is that you tried to separate the implementation of the class template into a source file, as would normally be done for classes. However, with templates, you generally have to put everything in the header file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2006, 11:00 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  5. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21