Thread: Templated classes...

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Templated classes...

    I am attempting to learn templated classes and functions, and figured with a templated class I could learn both easily.

    main.cpp -
    Code:
    #include <iostream>
    #include <string>
    #include "newtemplate.h"
    
    int main() {
        newtemplate<int> test(5);
        newtemplate<std::string> test2;
        test2.print("\nblah");
        return 0;
    }

    newtemplate.h -
    Code:
    #ifndef NEWTEMPLATE_H
    #define NEWTEMPLATE_H
    template <class type>
    class newtemplate {
        public:
            newtemplate() {};
            newtemplate(type a);
            ~newtemplate() {};
            void print(type a);
    };
    
    #endif //NEWTEMPLATE_H

    newtemplate.cpp -
    Code:
    #include <iostream>
    #include "newtemplate.h"
    
    template <class type> newtemplate<type>::newtemplate(type a) {
        std::cout<<a;
    }
    
    template <class type> void newtemplate<type>::print(type a) {
        std::cout<<a;
    }
    C:\DATA\development\cpp\newtemplate\newtemplate\ma in.cpp:6: undefined reference to `newtemplate<int>::newtemplate(int)'
    C:\DATA\development\cpp\newtemplate\newtemplate\ma in.cpp:8: undefined reference to `newtemplate<std::string>:rint(std::string)'
    :: === Build finished: 2 errors, 0 warnings ===
    I assume this is how it should be written from the FAQ on this site and a couple others... could someone point out my errors?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Implementations of templates must be in header files. For the reason, search the board.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I just saw why in the parashift C++ FAQ Lite, kinda annoying though, I am used to my definitions being in another file than the declarations... oh well.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    template <class type>
    I think that usage of the class keyword is deprecated in favor of typename.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am used to my definitions being in another file than the declarations... oh well.
    Technically a template is nothing more than a declaration. It's a template (hence the name) for a class or function where the parameters are replaced with explicit types or values provided by the user code.

    >I think that usage of the class keyword is deprecated in favor of typename.
    Some people may prefer typename over class (I'm one of them), but class is not deprecated for that use. It's a matter of style. Some people use typename when non-class types can be expected and class when only class types are expected. That's the only readability excuse I've seen that holds water, but I don't subscribe to it because it's misleading in that it looks like a constraint but really isn't.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I am used to my definitions being in another file than the declarations... oh well.
    One common technique is to leave the "definitions" in the source file and #include that at the bottom of the header file. Some would prefer that you use a different prefix (like .inl) for the source file in that case so as to avoid including a cpp file.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Ahh, nice idea Daved, thanks!

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Some compilers (g++ is among them) support extensions so they don't specifically require implementation of templates to be in header files. The catch, however, is that the techniques are compiler dependent, are typically disabled by default (i.e. if you want to do it, you need to enable specific compiler options), and typically rely on the project including a separate source file that (typically) #include's both the header and source file for the template and manually specialises templates for the types you use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing handlers (templated classes)
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 11:16 AM
  2. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  3. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM
  4. templated classes
    By homeyg in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2006, 09:07 AM
  5. Pointers to (templated) classes being declared
    By reanimated in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2006, 09:30 AM