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. troubles with templates

troubles with templates

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++wpftutorial
2 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.
  • K Offline
    K Offline
    Kamis
    wrote on last edited by
    #1

    Why does it work that way b) and not a)? a) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var); }; template void some_class::callfunc(T var) { } #endif b) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var){} }; #endif //main.cpp #include "SomeClass.h" void main(void) { some_class example; int nt=4; example.callfunc(nt); }

    D 1 Reply Last reply
    0
    • K Kamis

      Why does it work that way b) and not a)? a) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var); }; template void some_class::callfunc(T var) { } #endif b) // SomeClass.h #ifndef _SOMECLASS_ #define _SOMECLASS_ class some_class { public: some_class(){} ~some_class(){} template void callfunc(T var){} }; #endif //main.cpp #include "SomeClass.h" void main(void) { some_class example; int nt=4; example.callfunc(nt); }

      D Offline
      D Offline
      Diddy
      wrote on last edited by
      #2

      Because templates are expanded as they are compiled - just including the .h with the function declared (not defined) is not enough This is the pain the arse problem that you usually have. Really, its much easier if you stick to the thin template pattern - short template code. Then, put it in at the same time as decliration - as in b). If you really want to split it up - you can put the defination at the bottom of the .h its declared in. Or, if you really want the defination in a .CPP file - you can (uck) #include the .CPP into the other CPP you are using the template in.

      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