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. Templates and Unresolved Externals

Templates and Unresolved Externals

Scheduled Pinned Locked Moved C / C++ / MFC
helpwpf
5 Posts 3 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.
  • X Offline
    X Offline
    Xavier Shay
    wrote on last edited by
    #1

    I'm trying to implement a class not unlike CList, XList. I'm using a template template <class T> class XList and when I compile I get unresolved externals errors for my constructor and deconstructor. I originally got this error for all my functions but I fixed it by adding 'virtual' to the front of the declaration. This doesn't seem to work with the aforemention functions. I'm declaring them thus: XList(); virtual ~XList(); template <class T> XList<T>::XList() { // Constructor Code } template <class Tglt; XList<T>::~XList() { // Deconstructor code } Any help would be appreciated - X

    O 1 Reply Last reply
    0
    • X Xavier Shay

      I'm trying to implement a class not unlike CList, XList. I'm using a template template <class T> class XList and when I compile I get unresolved externals errors for my constructor and deconstructor. I originally got this error for all my functions but I fixed it by adding 'virtual' to the front of the declaration. This doesn't seem to work with the aforemention functions. I'm declaring them thus: XList(); virtual ~XList(); template <class T> XList<T>::XList() { // Constructor Code } template <class Tglt; XList<T>::~XList() { // Deconstructor code } Any help would be appreciated - X

      O Offline
      O Offline
      Oliver Anhuth
      wrote on last edited by
      #2

      Did you put the function definitions into the same file where your template class is declared (e.g. into the header file and not the .cpp file)? The compiler requires the functions definitions to be in the same translation unit than the class declaration. Oliver

      X K 2 Replies Last reply
      0
      • O Oliver Anhuth

        Did you put the function definitions into the same file where your template class is declared (e.g. into the header file and not the .cpp file)? The compiler requires the functions definitions to be in the same translation unit than the class declaration. Oliver

        X Offline
        X Offline
        Xavier Shay
        wrote on last edited by
        #3

        Thanks, it worked - X

        1 Reply Last reply
        0
        • O Oliver Anhuth

          Did you put the function definitions into the same file where your template class is declared (e.g. into the header file and not the .cpp file)? The compiler requires the functions definitions to be in the same translation unit than the class declaration. Oliver

          K Offline
          K Offline
          Krouer
          wrote on last edited by
          #4

          Did you mean that it's not possible to declare the class in a h file and the implementation in a cpp file? I have the same trouble as I convert my list which use void* to use template. But I don't move the code from cpp to h and when I link the library, it's ok. But when I link the application , I get link error. Does it exist another way to move the implementation from cpp to h for solving this kind of problem? Bruno

          O 1 Reply Last reply
          0
          • K Krouer

            Did you mean that it's not possible to declare the class in a h file and the implementation in a cpp file? I have the same trouble as I convert my list which use void* to use template. But I don't move the code from cpp to h and when I link the library, it's ok. But when I link the application , I get link error. Does it exist another way to move the implementation from cpp to h for solving this kind of problem? Bruno

            O Offline
            O Offline
            Oliver Anhuth
            wrote on last edited by
            #5

            When you use your template class in more than one source file you should put the class declaration and the function definition in the same header file. Most template libraries like STL, ATL, WTL ship without cpp files. Oliver

            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