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. stupid (template related?) linkage puzzle

stupid (template related?) linkage puzzle

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionlearning
5 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.
  • A Offline
    A Offline
    Alex Griffing
    wrote on last edited by
    #1

    Hi everyone! I've been programming in C++ for quite a while but I'm just learning the template stuff in the hopes of taking advantage of the STL. Anyway, I have a header file declaring two different classes, one of which is templated. The non-inlined member functions of these classes are defined in the same cpp file. When I try to build the program, the non-inlined member functions of the templated classes give me a linkage error (unresolved external symbol) while the corresponding functions of the non-templated class work fine. Furthermore, if I cut and paste the definitions of the member function of the templated class into the header file then everything links up fine. I know if I keep it like this I'll get reincarnated as a newt. Any suggestions? :confused:

    M E N A 4 Replies Last reply
    0
    • A Alex Griffing

      Hi everyone! I've been programming in C++ for quite a while but I'm just learning the template stuff in the hopes of taking advantage of the STL. Anyway, I have a header file declaring two different classes, one of which is templated. The non-inlined member functions of these classes are defined in the same cpp file. When I try to build the program, the non-inlined member functions of the templated classes give me a linkage error (unresolved external symbol) while the corresponding functions of the non-templated class work fine. Furthermore, if I cut and paste the definitions of the member function of the templated class into the header file then everything links up fine. I know if I keep it like this I'll get reincarnated as a newt. Any suggestions? :confused:

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      if I cut and paste the definitions of the member function of the templated class into the header file then everything links up fine. That's a known problem of the VC compiler. If you are stuck using VC you'll need to organize your code like that, and just look forward to being a newt. ;) --Mike-- http://home.inreach.com/mdunn/ "Holding the away team at bay with a non-functioning phaser was an act of unmitigated gall. I admire gall."   -- Lt. Cmdr. Worf

      1 Reply Last reply
      0
      • A Alex Griffing

        Hi everyone! I've been programming in C++ for quite a while but I'm just learning the template stuff in the hopes of taking advantage of the STL. Anyway, I have a header file declaring two different classes, one of which is templated. The non-inlined member functions of these classes are defined in the same cpp file. When I try to build the program, the non-inlined member functions of the templated classes give me a linkage error (unresolved external symbol) while the corresponding functions of the non-templated class work fine. Furthermore, if I cut and paste the definitions of the member function of the templated class into the header file then everything links up fine. I know if I keep it like this I'll get reincarnated as a newt. Any suggestions? :confused:

        E Offline
        E Offline
        Erik Funkenbusch
        wrote on last edited by
        #3

        That's the way templates are supposed to work. The ANSI C++ Committee created a special keyword, called "export" to allow you to put non-inlined functions outside of the translation unit, but no compiler yet supports this (not just VC). One work around is to #include the .cpp file at the end of your .h file, which is weird, but it will allow you to more easily port it at a later date when "export" becomes supported. The reason for this has to do with template expansion. Since the template expansion happens in the translation unit that is using the template, that means when your templates cpp file is compiled, it doesn't know what other template instantiations to create.

        1 Reply Last reply
        0
        • A Alex Griffing

          Hi everyone! I've been programming in C++ for quite a while but I'm just learning the template stuff in the hopes of taking advantage of the STL. Anyway, I have a header file declaring two different classes, one of which is templated. The non-inlined member functions of these classes are defined in the same cpp file. When I try to build the program, the non-inlined member functions of the templated classes give me a linkage error (unresolved external symbol) while the corresponding functions of the non-templated class work fine. Furthermore, if I cut and paste the definitions of the member function of the templated class into the header file then everything links up fine. I know if I keep it like this I'll get reincarnated as a newt. Any suggestions? :confused:

          N Offline
          N Offline
          NullStream
          wrote on last edited by
          #4

          With templates you have no choice. :( Sorry. I try to keep my paramatized classes as small as humanly possible. Sean Cody (NullStream) "As long as you want to live, everywhere will become heaven. Afterall, you are still alive." - End Of Evanglion

          1 Reply Last reply
          0
          • A Alex Griffing

            Hi everyone! I've been programming in C++ for quite a while but I'm just learning the template stuff in the hopes of taking advantage of the STL. Anyway, I have a header file declaring two different classes, one of which is templated. The non-inlined member functions of these classes are defined in the same cpp file. When I try to build the program, the non-inlined member functions of the templated classes give me a linkage error (unresolved external symbol) while the corresponding functions of the non-templated class work fine. Furthermore, if I cut and paste the definitions of the member function of the templated class into the header file then everything links up fine. I know if I keep it like this I'll get reincarnated as a newt. Any suggestions? :confused:

            A Offline
            A Offline
            Alex Griffing
            wrote on last edited by
            #5

            This is one of those problems that would have been a nagging doubt for a long time. Thanks for explaining it to me! :rose:

            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