stupid (template related?) linkage puzzle
-
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:
-
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:
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
-
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:
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.
-
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:
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
-
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:
This is one of those problems that would have been a nagging doubt for a long time. Thanks for explaining it to me! :rose: