Template

This is a discussion on Template within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am having trouble with class definition with template. The following is a simple example: Code: template <class ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Template

    Hi all,

    I am having trouble with class definition with template. The following is a simple example:
    Code:
    template <class DataType>
    struct TestStruct {
      DataType key;
    };
    
    template <class s, class d>
    class DummyClass {
    public:
      d getKey(s<d> *var) {
        return var->key = 10;
      }
    };
    
    int main() {
      DummyClass<TestStruct, double> myClas;
      return 0;
    }
    It produces an error because of s<d>.

    If I write:
    Code:
    template <class DataType>
    struct TestStruct {
     DataType key;
    };
    
    template <class s, class d>
    class DummyClass {
    public:
     d getKey(s *var) {
       return var->key = 10;
     }
    };
    
    int main() {
     DummyClass<TestStruct<double>, double> myClas;
     return 0;
    }
    It works just fine. But that is not I want because I need to use "double" twice. Can anyone help?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You want a template template:
    Code:
    template <class DataType>
    struct TestStruct {
      DataType key;
    };
    
    template <template <class T> class s, class d>
    class DummyClass {
    public:
      d getKey(s<d> *var) {
        return var->key = 10;
      }
    };
    
    int main() {
      DummyClass<TestStruct, double> myClas;
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    your suggestion does not work.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What errors are you getting? And what compiler are you using?

    Prelude's code is correct. (And I double checked this with GCC 3.3).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I use visual c++ version 6. There are many errors, but the first 2 are:
    error C2954: template definitions cannot nest
    error C2951: template declarations are only permitted at global or namespace scope

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    What compiler are you using? Most compilers have different deficiencies with regard to templates.

    If you cannot use a "better" compiler, you can always go with a less desirable option and specify the template parameter when instantiating your object:
    Code:
    template <class DataType>
    struct TestStruct {
      DataType key;
    };
    
    template <class s, class d>
    class DummyClass {
    public:
      d getKey(s *var) {
        return var->key = 10;
      }
    };
    
    int main() {
      DummyClass<TestStruct<double>, double> myClas;
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I originally coded that way, but I was thinking a user may erroneously instantiate
    Code:
    DummyClass<TestStruct<double>, int> myClas;
    which of course results in error(s). So to keep error to the minimal I figured removing redundant data-type declarations.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I use visual c++ version 6.
    Then you're screwed. Get a better compiler, or suffer the inadequacies of your trying to use a non-conforming compiler in a standard world.
    My best code is written with the delete key.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    MSVC 6 is an outdated compiler. It is not standards compliant (there was no standard when it was released), and it does things "its own way".

    GCC comes bundled with the Dev-C++ IDE.
    Also, I believe you can download MS compilers for free, but all you get is the command line compiler (no IDE -- you'll have to use something else for editing).

    Cheers

    *edit*
    Okay. Prelude beat me to the point and was more direct.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Thanx

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    24
    another approach is to provide a typedef within the template for any internal type you may want to make accessible to other templates:

    Code:
    template <class DataType>
    struct TestStruct {
     typedef DataType value_type;
     DataType key;
    };
    
    template <class s>
    class DummyClass {
    typedef typename s::value_type value_type;
    public:
      value_type getKey(s *var) {
       return s->key;
     }
    };
    
    int main() {
     DummyClass< TestStruct<double> > myClas;
     return 0;
    }
    Last edited by legend; 07-28-2005 at 06:42 PM.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    This one works! Moreover it is much compact. Thanx legend

  13. #13
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Umm.. Slightly offTopic, but aren't you suppose to use typename instead of class?

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    "class" is a synonym for "typename" within a template parameter declaration that is a type.

    So "template<class T>" == "template<typename T>".

    Found something interesting:
    http://blogs.msdn.com/slippman/archi...11/212768.aspx

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 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. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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