473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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<typename 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<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;
}
[/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\MICHAEL SCHMIDT\CBPROJECT\TEMPLATE\WINDOWS\DEBUG_BUILD\MAI N.OBJ

Error: Unresolved external 'Template<int>::getInfo() const' referenced from C:\DOCUMENTS AND SETTINGS\MICHAEL SCHMIDT\CBPROJECT\TEMPLATE\WINDOWS\DEBUG_BUILD\MAI N.OBJ
[/HTML]

Any help will be greatly apreciated!

Michael
Mar 3 '07 #1
4 1462
horace1
1,510 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. #include "Template.h"
  2.  
  3. template<typename T>
  4.  
  5. Template<T>::Template(T info){
  6.  
  7.   this->info = info;
  8.  
  9. }
  10.  
  11.  
  12.  
  13. template<typename T>
  14.  
  15. T Template<T>::getInfo()const{
  16.  
  17.   return this->info;
  18.  
  19. }
  20.  
  21.  
  22.  
  23. template<typename T>
  24.  
  25. void Template<T>::setInfo(T info){
  26.  
  27.   this->info = info;
  28.  
  29.  
  30. template class Template<int>;  // explicit instantiation of int version
  31.  
  32.  
Mar 3 '07 #2
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.
Mar 3 '07 #3
horace1
1,510 Expert 1GB
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!
Mar 3 '07 #4
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
Mar 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.