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. Class templates

Class templates

Scheduled Pinned Locked Moved C / C++ / MFC
helpasp-netdatabasewpfquestion
4 Posts 2 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.
  • 0 Offline
    0 Offline
    0v3rloader
    wrote on last edited by
    #1

    Hello, I am having trouble in compiling this project of mine. I will try to make myself clear so please bear with me for awhile... :) The scenario The workspace consists of 4 projects, 3 of them generating static libraries: - core.lib; - controls.lib ==> dependant on core.lib; - db.lib ==> also dependant on core.lib; The fourth project is the application which uses classes/objects from the 3 libraries I've just described. The problem I have just added a new class (CXList) to core.lib. This class allows the processing of a complex linked-list with variable data types - it is therefore a class template. Before including it in core.lib I tested it in a separate project and it performed just fine. But, when I included it in the master workspace described above and after having updated the whole project, I couldn't believe my eyes when I hit the Build button! I got loads of linking errors telling me that all (yes all) of the CXList objects (and methods) were not found in any of the libs (you know, the external object not found in db.lib kind of error). Is this some kind of a class template issue that I am not aware of? If you think you can shed some light on what I'm experiencing here, please do let me know. Thank you for your time, David Nimrod

    C 1 Reply Last reply
    0
    • 0 0v3rloader

      Hello, I am having trouble in compiling this project of mine. I will try to make myself clear so please bear with me for awhile... :) The scenario The workspace consists of 4 projects, 3 of them generating static libraries: - core.lib; - controls.lib ==> dependant on core.lib; - db.lib ==> also dependant on core.lib; The fourth project is the application which uses classes/objects from the 3 libraries I've just described. The problem I have just added a new class (CXList) to core.lib. This class allows the processing of a complex linked-list with variable data types - it is therefore a class template. Before including it in core.lib I tested it in a separate project and it performed just fine. But, when I included it in the master workspace described above and after having updated the whole project, I couldn't believe my eyes when I hit the Build button! I got loads of linking errors telling me that all (yes all) of the CXList objects (and methods) were not found in any of the libs (you know, the external object not found in db.lib kind of error). Is this some kind of a class template issue that I am not aware of? If you think you can shed some light on what I'm experiencing here, please do let me know. Thank you for your time, David Nimrod

      C Offline
      C Offline
      cmk
      wrote on last edited by
      #2

      By class template i assume you mean CXList is defined like : template<class TT> class CXList { ... }; If not then i don't know what you mean so stop reading. Ok, what you have defined with a template is an idea. There is NO CODE until you create a instance of the template by specifying the template parameters. e.g. CXList<int> blah; So even though the template code is included in code.lib project file no object code is produced when you compile. If you have specific instances of CXList that you want to export from core.lib then you can do the following : In a header file add : typedef CXList<int> CXListInt; // optional In a source file add : template class __declspec(dllexport) CXList; // required This will create all the instance code for CXList<int> and export all the methods. ...cmk Save the whales - collect the whole set

      0 1 Reply Last reply
      0
      • C cmk

        By class template i assume you mean CXList is defined like : template<class TT> class CXList { ... }; If not then i don't know what you mean so stop reading. Ok, what you have defined with a template is an idea. There is NO CODE until you create a instance of the template by specifying the template parameters. e.g. CXList<int> blah; So even though the template code is included in code.lib project file no object code is produced when you compile. If you have specific instances of CXList that you want to export from core.lib then you can do the following : In a header file add : typedef CXList<int> CXListInt; // optional In a source file add : template class __declspec(dllexport) CXList; // required This will create all the instance code for CXList<int> and export all the methods. ...cmk Save the whales - collect the whole set

        0 Offline
        0 Offline
        0v3rloader
        wrote on last edited by
        #3

        cmk wrote: In a source file add : template class __declspec(dllexport) CXList; // required That's precisely what I mean. cmk wrote: By class template i assume you mean CXList is defined like : template class CXList { ... }; Ahh! The magic code to make it work! I thought the compiler did this automatically. Can you provide me with an explanation as to why you have to do it like so? Thanks a lot, cmk. You have been very helpful indeed! David Nimrod

        C 1 Reply Last reply
        0
        • 0 0v3rloader

          cmk wrote: In a source file add : template class __declspec(dllexport) CXList; // required That's precisely what I mean. cmk wrote: By class template i assume you mean CXList is defined like : template class CXList { ... }; Ahh! The magic code to make it work! I thought the compiler did this automatically. Can you provide me with an explanation as to why you have to do it like so? Thanks a lot, cmk. You have been very helpful indeed! David Nimrod

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          Whoops, i made a typo template class __declspec(dllexport) CXList; should be, template class __declspec(dllexport) CXList<int>; dNimrod#X wrote: Can you provide me with an explanation as to why you have to do it like so? Because a template is just that - a template that the compiler can use to create code at compile time when you specialize the template. When you define the template class template<class TT> MyClassT { ... }; The TT is a variable/placeholder. What do you expect the compiler to do with TT unless you specify what it is ? But, when you say MyClass<int> the compiler goes 'ahhh, now i can replace all cases of TT with int and compile'. As for exporting, given the above you can't export a template class like you would a normal class. So the syntax i provided was the solution 'they' decided on. It tells the compiler that, even if CXList<int> isn't used anywhere, you want it to create all methods and export them. If you didn't do this the compiler would optimize away the code. The same syntax can be used to export function template instances, or specific class template method instances (ie you don't want to export all methods from a class template). ...cmk Save the whales - collect the whole set

          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