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. What's wrong with my code? (class template)

What's wrong with my code? (class template)

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
7 Posts 5 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.
  • L Offline
    L Offline
    Li Lirong
    wrote on last edited by
    #1

    I have the following codes, all of them are in the same file "test.cpp" //declaration template <class T> class temp { public:     class nested         {         public:             void test();         };     void test()     {         nested nes;         nes.test();     } }; //definition template <class T> void temp<T>::nested::test() {} int main( /* int argc, char* argv[] */ ) {     temp<int> tem;     tem.test();     return 0; } error message: test.obj : error LNK2001: unresolved external symbol "public: void __thiscall temp<int>::nested::test(void)" (?test@nested@?$temp@H@@QAEXXZ) test.exe : fatal error LNK1120: 1 unresolved externals what's wrong with my code? thanks :(( Lirong

    V J 2 Replies Last reply
    0
    • L Li Lirong

      I have the following codes, all of them are in the same file "test.cpp" //declaration template <class T> class temp { public:     class nested         {         public:             void test();         };     void test()     {         nested nes;         nes.test();     } }; //definition template <class T> void temp<T>::nested::test() {} int main( /* int argc, char* argv[] */ ) {     temp<int> tem;     tem.test();     return 0; } error message: test.obj : error LNK2001: unresolved external symbol "public: void __thiscall temp<int>::nested::test(void)" (?test@nested@?$temp@H@@QAEXXZ) test.exe : fatal error LNK1120: 1 unresolved externals what's wrong with my code? thanks :(( Lirong

      V Offline
      V Offline
      Vivek Rajan
      wrote on last edited by
      #2

      Lirong - Pls try reposting with "<" and ">" for the < and > symbols. Some key statements are missing.. But I think I might know the answer, some compilers are really anal about this stuff. They expect all template functions to be defined in one file (.h or .cpp), in other words ONE Translation Unit.. (boo hoo !! ) I suspect VC++ is one of those compilers. ( Maybe an expert can confirm). So, try writing the "test" function of the inner class within the class defn. things should be ok. Eg. ... class nested { public: void test() { // put your function body here }; ... Hooe this helps- Vivek

      L 1 Reply Last reply
      0
      • V Vivek Rajan

        Lirong - Pls try reposting with "<" and ">" for the < and > symbols. Some key statements are missing.. But I think I might know the answer, some compilers are really anal about this stuff. They expect all template functions to be defined in one file (.h or .cpp), in other words ONE Translation Unit.. (boo hoo !! ) I suspect VC++ is one of those compilers. ( Maybe an expert can confirm). So, try writing the "test" function of the inner class within the class defn. things should be ok. Eg. ... class nested { public: void test() { // put your function body here }; ... Hooe this helps- Vivek

        L Offline
        L Offline
        Li Lirong
        wrote on last edited by
        #3

        Thanks. ;) I have made some correctness on my question; Yes, If I write the definition of "test" of the nested class within the class declaration, it does work. So my question is: Is it possible to separate the definition and declaration of a function of a nested class ( which is nested in a template ). Is there anything wrong with my syntax? Or it is just because of the compiler(vc++6)? Thanks. Lirong

        L G 2 Replies Last reply
        0
        • L Li Lirong

          Thanks. ;) I have made some correctness on my question; Yes, If I write the definition of "test" of the nested class within the class declaration, it does work. So my question is: Is it possible to separate the definition and declaration of a function of a nested class ( which is nested in a template ). Is there anything wrong with my syntax? Or it is just because of the compiler(vc++6)? Thanks. Lirong

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Lirong - Unfortunately, if you have a class template.. you gotta put the nested method code within the class definition. (ie *.h file) You did nothing wrong. It is just VC++ 6.0. ( actually many other compilers are also guilty) Later- Vivek

          1 Reply Last reply
          0
          • L Li Lirong

            Thanks. ;) I have made some correctness on my question; Yes, If I write the definition of "test" of the nested class within the class declaration, it does work. So my question is: Is it possible to separate the definition and declaration of a function of a nested class ( which is nested in a template ). Is there anything wrong with my syntax? Or it is just because of the compiler(vc++6)? Thanks. Lirong

            G Offline
            G Offline
            Ghazi H Wadi
            wrote on last edited by
            #5

            Problem is that The compiler does not generate code for the member function in the nested class template. It is a bug with MS VC <= 6.0 see here "ID: Q128789" To work around it do this define your class internally to the nested class

            #include #include "header.h"
            //declaration
            template class temp
            {
            public:
            class nested
            {
            public:
            void test()
            {
            cout << "Hello world"<< endl;
            }
            };

            void test()
            {
                nested nes;
                nes.test();
            }
            

            };

            int main( /* int argc, char* argv[] */ )
            {
            temp tem;
            tem.test();
            return 0;
            }

            Hope that helped. Cheers Alfadhly It is Illogical to define an inventor by his invention

            L 1 Reply Last reply
            0
            • G Ghazi H Wadi

              Problem is that The compiler does not generate code for the member function in the nested class template. It is a bug with MS VC <= 6.0 see here "ID: Q128789" To work around it do this define your class internally to the nested class

              #include #include "header.h"
              //declaration
              template class temp
              {
              public:
              class nested
              {
              public:
              void test()
              {
              cout << "Hello world"<< endl;
              }
              };

              void test()
              {
                  nested nes;
                  nes.test();
              }
              

              };

              int main( /* int argc, char* argv[] */ )
              {
              temp tem;
              tem.test();
              return 0;
              }

              Hope that helped. Cheers Alfadhly It is Illogical to define an inventor by his invention

              L Offline
              L Offline
              Li Lirong
              wrote on last edited by
              #6

              Thank all.

              ;)

              I have try it in C++ Builder. It runs well there.

              I could have defined the function within the class declaration. However, I am not able to do that. The reason is illustrated by the following example. In the example, A<T>::nested uses B<T> and B<T>::nested used A<T>.

              // begin of A.h
              #pragma once

              // foreword declaration, but we still can not use class B.
              //that is why I can't put the definition of A<T>::nested::test() inside the declaration;

              template <class T> class B;

              template <class T> class A

              {

              public:

              class nested

              {

              public:

              void test(B<T> b);

              };

              nested GetNested() const

              {

              return nested();

              }

              }

              #include A.imp

              // end of A.h

              <

              1 Reply Last reply
              0
              • L Li Lirong

                I have the following codes, all of them are in the same file "test.cpp" //declaration template <class T> class temp { public:     class nested         {         public:             void test();         };     void test()     {         nested nes;         nes.test();     } }; //definition template <class T> void temp<T>::nested::test() {} int main( /* int argc, char* argv[] */ ) {     temp<int> tem;     tem.test();     return 0; } error message: test.obj : error LNK2001: unresolved external symbol "public: void __thiscall temp<int>::nested::test(void)" (?test@nested@?$temp@H@@QAEXXZ) test.exe : fatal error LNK1120: 1 unresolved externals what's wrong with my code? thanks :(( Lirong

                J Offline
                J Offline
                Jim Crafton
                wrote on last edited by
                #7

                You can't do the implementation of temp::nested::test() like you're doing. Even though Bjarne say's it's possible (C++ 3rd edition), the VC++ compiler does NOT support this. All the implementation MUST be in line - i.e. //declaration template class temp { public: class nested { public: //this must be implemented here void test() { //whatever } }; void test() { nested nes; nes.test(); } }; Hope this helps ! Jim Crafton

                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