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. Problem in writing template function inside class

Problem in writing template function inside class

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomlinuxquestion
10 Posts 4 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.
  • N Offline
    N Offline
    Naveen
    wrote on last edited by
    #1

    Hi all, I have a template function inside a class a shown below. But while compiling I am getting an error "error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int'". Anybody knows whats wrong with the code? class Test { public: template<class T> void someFunction( T obj ); }; template<class T> void Test::someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; Test obj; obj.someFunction( n );// error comes here return 0; } Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like.. class Test { public: template<class T> void someFunction( T obj )// no error { } };

    nave [OpenedFileFinder]

    M M 2 Replies Last reply
    0
    • N Naveen

      Hi all, I have a template function inside a class a shown below. But while compiling I am getting an error "error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int'". Anybody knows whats wrong with the code? class Test { public: template<class T> void someFunction( T obj ); }; template<class T> void Test::someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; Test obj; obj.someFunction( n );// error comes here return 0; } Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like.. class Test { public: template<class T> void someFunction( T obj )// no error { } };

      nave [OpenedFileFinder]

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      It should be:

      template <class T>
      class Test
      {
      void function(T);
      };


      Maxwell Chen

      modified on Friday, December 28, 2007 4:42:31 AM

      N 1 Reply Last reply
      0
      • M Maxwell Chen

        It should be:

        template <class T>
        class Test
        {
        void function(T);
        };


        Maxwell Chen

        modified on Friday, December 28, 2007 4:42:31 AM

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        No no thats not possible. I only need to call the function with different types. I think the problem is with the vc6. In vc8, it is working fine.

        nave [OpenedFileFinder]

        S M 2 Replies Last reply
        0
        • N Naveen

          No no thats not possible. I only need to call the function with different types. I think the problem is with the vc6. In vc8, it is working fine.

          nave [OpenedFileFinder]

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          Naveen, It's a bug of Microsoft Visual C++ (6.0) compiler. There's no workaround available. They have corrected it in the newer versions. Please check KB article[^]

          -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

          My blog - Sharing My Thoughts, An Article - Understanding Statepattern

          1 Reply Last reply
          0
          • N Naveen

            Hi all, I have a template function inside a class a shown below. But while compiling I am getting an error "error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int'". Anybody knows whats wrong with the code? class Test { public: template<class T> void someFunction( T obj ); }; template<class T> void Test::someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; Test obj; obj.someFunction( n );// error comes here return 0; } Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like.. class Test { public: template<class T> void someFunction( T obj )// no error { } };

            nave [OpenedFileFinder]

            M Offline
            M Offline
            Mahesh Kulkarni
            wrote on last edited by
            #5

            Hi Naveen , You are right the code given by you is working on VC7 and onwards But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you. template<class T> class Test { public: void someFunction( T obj ); }; template<class T> void **Test<T>::**someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; **Test<int>** obj; obj.someFunction( n );// error comes here return 0; }

            The secret of life is not enjoyment but education through experience. - Swami Vivekananda.

            N M 2 Replies Last reply
            0
            • N Naveen

              No no thats not possible. I only need to call the function with different types. I think the problem is with the vc6. In vc8, it is working fine.

              nave [OpenedFileFinder]

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #6

              Oh I misunderstood your question. Sorry!


              Maxwell Chen

              1 Reply Last reply
              0
              • M Mahesh Kulkarni

                Hi Naveen , You are right the code given by you is working on VC7 and onwards But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you. template<class T> class Test { public: void someFunction( T obj ); }; template<class T> void **Test<T>::**someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; **Test<int>** obj; obj.someFunction( n );// error comes here return 0; }

                The secret of life is not enjoyment but education through experience. - Swami Vivekananda.

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                Hi Mahesh , The problem with your sugeestion is that, I cannot call the obj.someFunction() with both int and long at the same time like.. int n = 0; long l = 0; obj.someFunction( n ); obj.someFunction( l );

                nave [OpenedFileFinder]

                1 Reply Last reply
                0
                • M Mahesh Kulkarni

                  Hi Naveen , You are right the code given by you is working on VC7 and onwards But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you. template<class T> class Test { public: void someFunction( T obj ); }; template<class T> void **Test<T>::**someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; **Test<int>** obj; obj.someFunction( n );// error comes here return 0; }

                  The secret of life is not enjoyment but education through experience. - Swami Vivekananda.

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #8

                  I think that Naveen wants (member) function templates of a class, not class templates. If it is a class template, the argument type is fixed when the class object is declared. So we can not invoke member functions with different input types (something like function overloading).


                  Maxwell Chen

                  M 1 Reply Last reply
                  0
                  • M Maxwell Chen

                    I think that Naveen wants (member) function templates of a class, not class templates. If it is a class template, the argument type is fixed when the class object is declared. So we can not invoke member functions with different input types (something like function overloading).


                    Maxwell Chen

                    M Offline
                    M Offline
                    Mahesh Kulkarni
                    wrote on last edited by
                    #9

                    HI Naveen and MaxWell I am really sorry ..It was my misunderstanding. later I worked for wht Naveen exactly want, on VC6 But it was not working then i googled, I found the Following link http://forums.devx.com/archive/index.php/t-82949.html[^]

                    The secret of life is not enjoyment but education through experience. - Swami Vivekananda.

                    M 1 Reply Last reply
                    0
                    • M Mahesh Kulkarni

                      HI Naveen and MaxWell I am really sorry ..It was my misunderstanding. later I worked for wht Naveen exactly want, on VC6 But it was not working then i googled, I found the Following link http://forums.devx.com/archive/index.php/t-82949.html[^]

                      The secret of life is not enjoyment but education through experience. - Swami Vivekananda.

                      M Offline
                      M Offline
                      Maxwell Chen
                      wrote on last edited by
                      #10

                      To tell the truth, I have been 7 years not coding template stuffs. I just tried hard to recall everything about templates (both member function template and class template), and tried with VC++2008. It works well finally. ;P


                      Maxwell Chen

                      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