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. C++ templates

C++ templates

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfquestion
7 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.
  • M Offline
    M Offline
    minkowski
    wrote on last edited by
    #1

    Hi, I have found out (the hard way!) that you can't have a .cpp file with your function definitions if you are using a template class. All your function definitions must be in the .h file. Why is this? Thanks for any info.

    J R N 3 Replies Last reply
    0
    • M minkowski

      Hi, I have found out (the hard way!) that you can't have a .cpp file with your function definitions if you are using a template class. All your function definitions must be in the .h file. Why is this? Thanks for any info.

      J Offline
      J Offline
      Jonathan Darka
      wrote on last edited by
      #2

      You can put the code in a C++ file, you just need to change how you define your templates. I would not recommend that though, justuse a header file it's much easier. regards,


      Jonathan Wilkes Darka[Xanya.net]

      M 1 Reply Last reply
      0
      • J Jonathan Darka

        You can put the code in a C++ file, you just need to change how you define your templates. I would not recommend that though, justuse a header file it's much easier. regards,


        Jonathan Wilkes Darka[Xanya.net]

        M Offline
        M Offline
        minkowski
        wrote on last edited by
        #3

        Hi, Thanks for that, but do you know why the C++ compilier objects to having function definitions in the .cpp ? Thanks.

        C J 2 Replies Last reply
        0
        • M minkowski

          Hi, Thanks for that, but do you know why the C++ compilier objects to having function definitions in the .cpp ? Thanks.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          AFAIK it is because template code is generated by your compiler. It means that when you have a template instantiation (something like list<float> ) your compiler will generate code corresponding to this template type. To be able to generate the code, it needs the complete function implementation.


          Cédric Moonen Software developer
          Charting control [v1.2]

          1 Reply Last reply
          0
          • M minkowski

            Hi, I have found out (the hard way!) that you can't have a .cpp file with your function definitions if you are using a template class. All your function definitions must be in the .h file. Why is this? Thanks for any info.

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

            Yes, all in the .h file. Only specializations must be putted in the .cpp file (declaraion, of course, in the .h file).

            minkowski wrote:

            Why is this?

            I think that this has only a reason related to how the compiler works to elaborates the templates. I hope that this isn't a problem for you.:~


            Russell

            1 Reply Last reply
            0
            • M minkowski

              Hi, Thanks for that, but do you know why the C++ compilier objects to having function definitions in the .cpp ? Thanks.

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              You do *not* have to have definitions in th ecpp. You do this to (partially) separate interface and implementation. With templates, every instantiation (for type A, type B etc) creates a new, distinct type: vector<A> is different from vector<B>. And for this instantiation, the compiler needs the complete definition of the template. Therefore it is easiest to put the template in the header, to have it ready when you need it.


              Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
              George Orwell, "Keep the Aspidistra Flying", Opening words

              1 Reply Last reply
              0
              • M minkowski

                Hi, I have found out (the hard way!) that you can't have a .cpp file with your function definitions if you are using a template class. All your function definitions must be in the .h file. Why is this? Thanks for any info.

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #7

                minkowski wrote:

                Why is this?

                See here[^]


                Programming Blog utf8-cpp

                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