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 Troubles (Solved)

Template Troubles (Solved)

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpsharepointwpfgraphics
9 Posts 6 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.
  • R Offline
    R Offline
    Raskolnikov
    wrote on last edited by
    #1

    I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?

    S J R T 5 Replies Last reply
    0
    • R Raskolnikov

      I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?

      S Offline
      S Offline
      sultan_of_6string
      wrote on last edited by
      #2

      Well, your syntax for using templates is wrong. The template declaration is as follows:

      template<class mytype, ...>
      class MyClass
      {
      ...

      This allows you to use a type called mytype or whatever. The class' member functions should be declared as follows:

      template<class mytype, ...>
      MyClass<mytype, ...>::MemberFunction

      Finally, declare instances of the template class like this:

      myClass<type (int, class, CString, whatever)> myVar;

      This is the worst explanation of template syntax ever. You should read the MSDN documentation of templates. I'm surprised the code got through the compiler.

      R 1 Reply Last reply
      0
      • R Raskolnikov

        I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?

        J Offline
        J Offline
        jbarton
        wrote on last edited by
        #3

        First, you probably should send the source again with the "Display this message as-is (no HTML)" checked, so that it can be read. Second, you need to have the template code available when you are compiling main.cpp - if you just have the template code in test.cpp but no actual code using it for a specific type, nothing gets expanded. Typically, you would put your template code in the test.h include file. You should not have a test.cpp file for this template class. Any source file that needs to use the templated class can then include test.h, and the templated functions will be expanded as needed. Best regards, John

        1 Reply Last reply
        0
        • S sultan_of_6string

          Well, your syntax for using templates is wrong. The template declaration is as follows:

          template<class mytype, ...>
          class MyClass
          {
          ...

          This allows you to use a type called mytype or whatever. The class' member functions should be declared as follows:

          template<class mytype, ...>
          MyClass<mytype, ...>::MemberFunction

          Finally, declare instances of the template class like this:

          myClass<type (int, class, CString, whatever)> myVar;

          This is the worst explanation of template syntax ever. You should read the MSDN documentation of templates. I'm surprised the code got through the compiler.

          R Offline
          R Offline
          redeemer
          wrote on last edited by
          #4

          it's because everything between a pair of tags, <>, is interpreted as an html tag and therefore remove cause he forgot to turn off html interpretation.

          S 1 Reply Last reply
          0
          • R Raskolnikov

            I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?

            R Offline
            R Offline
            Raskolnikov
            wrote on last edited by
            #5

            Sorry this is my first post and I did not notice the <> disapearing. I found the answer to my problem at another forum. Thanks for the help.

            P 1 Reply Last reply
            0
            • R redeemer

              it's because everything between a pair of tags, <>, is interpreted as an html tag and therefore remove cause he forgot to turn off html interpretation.

              S Offline
              S Offline
              sultan_of_6string
              wrote on last edited by
              #6

              Hehe!;)

              1 Reply Last reply
              0
              • R Raskolnikov

                Sorry this is my first post and I did not notice the <> disapearing. I found the answer to my problem at another forum. Thanks for the help.

                P Offline
                P Offline
                Paul M Watt
                wrote on last edited by
                #7

                If you post your answer, then other people can learn from this post as well. What was your solution?


                Build a man a fire, and he will be warm for a day
                Light a man on fire, and he will be warm for the rest of his life!

                1 Reply Last reply
                0
                • R Raskolnikov

                  I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  That isn't specific to VC6. I don't even know of a compiler that supports templates where the definition isn't fully known to the module in question. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                  1 Reply Last reply
                  0
                  • R Raskolnikov

                    I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?

                    R Offline
                    R Offline
                    Raskolnikov
                    wrote on last edited by
                    #9

                    The solution is to put the implementation in the .h file. I tried putting it in a .cpp and VC++ added it to the project resulting in multiple definitions. I even tried inclusion guards (#ifndef). So itstead I left it in the .h, .txt workes as well (I really don't like putting the implementation in a header file).

                    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