How to use templates propperly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad5000
    New Member
    • Sep 2006
    • 22

    How to use templates propperly

    Helo I´m having trouble finding out how to use templates properly. Te problem I´m having is, that I can´t find the right way to make the templates work outside the file where the class using the template is declared. Look at the following example.

    Template.h file
    [PHP]
    #ifndef TEMPLATE_H
    #define TEMPLATE_H

    template<typena me T>
    class Template{
    private:
    T info;
    public:
    Template(T info);
    T getInfo()const;
    void setInfo(T info);
    };

    #endif
    [/PHP]

    Template.cpp file
    [PHP]
    #include "Template.h "

    template<typena me T>
    Template<T>::Te mplate(T info){
    this->info = info;
    }

    template<typena me T>
    T Template<T>::ge tInfo()const{
    return this->info;
    }

    template<typena me T>
    void Template<T>::se tInfo(T info){
    this->info = info;
    }
    [/PHP]

    [PHP]
    #include <iostream>
    #include "Template.h "
    using namespace std;
    int main(){
    Template< int > t(1);
    cout<<t.getInfo ()<<endl;
    }
    [/PHP]

    the error message the compiler tells me is the following:
    [HTML]
    Error: Unresolved external 'Template<int>: :Template<int>( int)' referenced from C:\DOCUMENTS AND SETTINGS\MICHAE L SCHMIDT\CBPROJE CT\TEMPLATE\WIN DOWS\DEBUG_BUIL D\MAIN.OBJ

    Error: Unresolved external 'Template<int>: :getInfo() const' referenced from C:\DOCUMENTS AND SETTINGS\MICHAE L SCHMIDT\CBPROJE CT\TEMPLATE\WIN DOWS\DEBUG_BUIL D\MAIN.OBJ
    [/HTML]

    Any help will be greatly apreciated!

    Michael
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the problem is with template instantiation where you have to generate an explict definition of a class from a template, see
    http://www.is.pku.edu. cn/~qzy/cpp/vc-stl/templates.htm

    for example the following template.cpp works with your main() with the gcc compiler
    Code:
    #include "Template.h"
    
    template<typename T>
    
    Template<T>::Template(T info){
    
      this->info = info;
    
    }
    
    
    
    template<typename T>
    
    T Template<T>::getInfo()const{
    
      return this->info;
    
    }
    
    
    
    template<typename T>
    
    void Template<T>::setInfo(T info){
    
      this->info = info;
    
    } 
    
    template class Template<int>;  // explicit instantiation of int version

    Comment

    • nomad5000
      New Member
      • Sep 2006
      • 22

      #3
      this is verry nice and I already knew about it, but there has to be a way where you dont have to use this
      [HTML]
      template class Template<int>;// explicit instantiation of int version
      [/HTML]
      What I want to do is write a Class for example a Stack class that I can use with whatever type of data without having to explicitly instantiate.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by nomad5000
        this is verry nice and I already knew about it, but there has to be a way where you dont have to use this
        [HTML]
        template class Template<int>;// explicit instantiation of int version
        [/HTML]
        What I want to do is write a Class for example a Stack class that I can use with whatever type of data without having to explicitly instantiate.
        a template has to be instantiated for a particular type at some point - how you do it can depend on the compiler - I gave the example using gcc which I mainly use. What compiler are you using? if you do a google search on template instantiation you will find plenty of discussions!

        Comment

        • nomad5000
          New Member
          • Sep 2006
          • 22

          #5
          I'm using Borland Win 32 compiler tools
          probably the problem is, that mycompiler doesn't suport automatic template instanciation. Something that I find verry anoing. What point does it have to use templates if you cant use them for a type without instancieting them first.

          Thank you for your help!

          regards
          Michael

          Comment

          Working...