Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template classes

Template classes

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
18 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T toxcct

    template<class _T>
    _T TCollection::at(unsigned int iPos){...}

    template<class _T>
    void TCollection::add(_T T){...}


    TOXCCT >>> GEII power

    [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

    W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #7

    Thanks for your help with this, but I am still getting errors.

    template<class _T> _T TCollection::at(unsigned int iPos)
    {
    ...
    }

    template<class _T> void TCollection::add(_T T)

    Results in the first error: E:\Program\foo\TCollection.cpp(17) : error C2955: 'TCollection' : use of class template requires template argument list e:\program\foo\tcollection.h(24) : see declaration of 'TCollection' E:\Program\foo\TCollection.cpp(25) : error C2955: 'TCollection' : use of class template requires template argument list

    T 1 Reply Last reply
    0
    • W Waldermort

      Thanks for your help with this, but I am still getting errors.

      template<class _T> _T TCollection::at(unsigned int iPos)
      {
      ...
      }

      template<class _T> void TCollection::add(_T T)

      Results in the first error: E:\Program\foo\TCollection.cpp(17) : error C2955: 'TCollection' : use of class template requires template argument list e:\program\foo\tcollection.h(24) : see declaration of 'TCollection' E:\Program\foo\TCollection.cpp(25) : error C2955: 'TCollection' : use of class template requires template argument list

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #8

      template<class _T>
      _T TCollection**<_T>**::at(unsigned int iPos){...}

      template<class _T>
      void TCollection**<_T>**::add(_T T) {...}


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      W 1 Reply Last reply
      0
      • T toxcct

        template<class _T>
        _T TCollection**<_T>**::at(unsigned int iPos){...}

        template<class _T>
        void TCollection**<_T>**::add(_T T) {...}


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #9

        Thankyou that worked, but now having problems with the pData pointer. I thought it would be easy to use seperate files. Rather than messing about I have placed the whole thing in the single header file, it's only a small class.

        T 1 Reply Last reply
        0
        • W Waldermort

          Thankyou that worked, but now having problems with the pData pointer. I thought it would be easy to use seperate files. Rather than messing about I have placed the whole thing in the single header file, it's only a small class.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #10

          template classes inmplementations have to be in the header... what is the problem with pData exactly ?


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          W Z 2 Replies Last reply
          0
          • T toxcct

            template classes inmplementations have to be in the header... what is the problem with pData exactly ?


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            W Offline
            W Offline
            Waldermort
            wrote on last edited by
            #11

            I can't remember eaxactly after putting it back in the header, it was something about an unknown class size when trying to delete it. Basicaly this is just a template similar to the vector class, with the exception it's smaller and calls the destructor.

            1 Reply Last reply
            0
            • W Waldermort

              I have defined a template class in a header file like so:

              template <class _T> class TCollection {
              public:
              TCollection();
              ~TCollection();

              Then try to declare the members in the cpp file like so:

              TCollection::TCollection() : pData(0) , pCount(0)
              {
              }

              TCollection::~TCollection()
              {
              clear();
              }

              Yet I am getting a compiler error 'TCollection' : use of class template requires template argument list Obviously it cannot be treated like a normal class, so what is the correct way of declaring the members?

              A Offline
              A Offline
              Anonymuos
              wrote on last edited by
              #12

              waldermort wrote:

              I have defined a template class in a header file like so:

              It's a class template, not a template class.

              waldermort wrote:

              Then try to declare the members in the cpp file like so:

              You cannot define template members in a cpp file. Look up the C++ FAQ!

              W 1 Reply Last reply
              0
              • W Waldermort

                I have defined a template class in a header file like so:

                template <class _T> class TCollection {
                public:
                TCollection();
                ~TCollection();

                Then try to declare the members in the cpp file like so:

                TCollection::TCollection() : pData(0) , pCount(0)
                {
                }

                TCollection::~TCollection()
                {
                clear();
                }

                Yet I am getting a compiler error 'TCollection' : use of class template requires template argument list Obviously it cannot be treated like a normal class, so what is the correct way of declaring the members?

                M Offline
                M Offline
                MayankT
                wrote on last edited by
                #13

                When defining you must explicitly declare the function as template: template <class _T> TCollection<_T>::TCollection () : pData(0), pCount(0) { } template <class _T> TCollection<_T>::~TCollection () { clear(); }

                ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                T 1 Reply Last reply
                0
                • A Anonymuos

                  waldermort wrote:

                  I have defined a template class in a header file like so:

                  It's a class template, not a template class.

                  waldermort wrote:

                  Then try to declare the members in the cpp file like so:

                  You cannot define template members in a cpp file. Look up the C++ FAQ!

                  W Offline
                  W Offline
                  Waldermort
                  wrote on last edited by
                  #14

                  Well I do appologise if my English offends you, although it appears the other members (forum members that is and not class members before you get confused) here did not have such a hard time understanding me. May I ask, Mr.Anonymuos, do you use the same attitude when your customers come to you with a problem?

                  1 Reply Last reply
                  0
                  • M MayankT

                    When defining you must explicitly declare the function as template: template <class _T> TCollection<_T>::TCollection () : pData(0), pCount(0) { } template <class _T> TCollection<_T>::~TCollection () { clear(); }

                    ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #15

                    well, you're right. indeed, i think i already answered him (if you read the previous posts...)


                    TOXCCT >>> GEII power

                    [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                    1 Reply Last reply
                    0
                    • T toxcct

                      template classes inmplementations have to be in the header... what is the problem with pData exactly ?


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #16

                      toxcct wrote:

                      template classes inmplementations have to be in the header.

                      This is only because there is no compiler out there that meets this part of the standard (mainly due to the insane complexity it takes to accomplish it). The standard allows for placing template definitions in header files and implementations in cpp files. To get around this problem, I generally either put the entire class definition and implementation into a single file, or I place a #include "myclass.cpp" at the bottom of my header file.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      T 1 Reply Last reply
                      0
                      • Z Zac Howland

                        toxcct wrote:

                        template classes inmplementations have to be in the header.

                        This is only because there is no compiler out there that meets this part of the standard (mainly due to the insane complexity it takes to accomplish it). The standard allows for placing template definitions in header files and implementations in cpp files. To get around this problem, I generally either put the entire class definition and implementation into a single file, or I place a #include "myclass.cpp" at the bottom of my header file.

                        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #17

                        yes i know that, but that fact is that actually, so i didn't want to pollute the post with something that wouldn't have helped...


                        TOXCCT >>> GEII power

                        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                        1 Reply Last reply
                        0
                        • W Waldermort

                          I have defined a template class in a header file like so:

                          template <class _T> class TCollection {
                          public:
                          TCollection();
                          ~TCollection();

                          Then try to declare the members in the cpp file like so:

                          TCollection::TCollection() : pData(0) , pCount(0)
                          {
                          }

                          TCollection::~TCollection()
                          {
                          clear();
                          }

                          Yet I am getting a compiler error 'TCollection' : use of class template requires template argument list Obviously it cannot be treated like a normal class, so what is the correct way of declaring the members?

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #18

                          Separating template classes into .h and .cpp files is too much of a headache when you're just starting out with templates. It's far easier to put everything in the .h file. See the C++ FAQ Lite[^] template section for more details.

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups