Template in *.h or *.cpp

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 8
Reputation: rkarimi is an unknown quantity at this point 
Solved Threads: 0
rkarimi rkarimi is offline Offline
Newbie Poster

Template in *.h or *.cpp

 
0
  #1
Jun 30th, 2005
Hi,

Assume that we want to define a set of template function separately & then use them in other files, by calling their header.

1. Shoud we put function body (both define & declare) in header file as well. My current code works this way. But when I put those body in the *.cpp file then it is compiled & linked without error. But crashes run time!
2. I need to use some of those function in other file as well. But if I put function body in *.h files then I recieve mutliple definition error when linking (I already have one #ifnedef block covering all mu header code)

Any suggestion/Alternative?

Thanks in advance,
Reza
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: Template in *.h or *.cpp

 
0
  #2
Jun 30th, 2005
hey
according to me the program crashing should be a different reason as i have included a cpp file in programs and run them without error.. post the problematic code..
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: rkarimi is an unknown quantity at this point 
Solved Threads: 0
rkarimi rkarimi is offline Offline
Newbie Poster

Re: Template in *.h or *.cpp, Question Untouch?

 
0
  #3
Jun 30th, 2005
Hello,
Let's not refer to ourselve but to compiler!
Try this:
__________________________
File 1: Main.cpp
__________________________
#include <stdio.h>
#include "ADD2.h"

int main (int argc, char * const argv[]) {
int a=3;
float b=3.3;
printf("\na=%d \t ADD2(a)=%d",a,ADD2(a));
printf("\nb=%f \t ADD2(b)=%f",b,ADD2(b));
}
__________________________
File 2: ADD2.h
__________________________
#ifndef ADD2_H
#define ADD2_H
template <class T>
T ADD2(T a);

template <class T>
T ADD2(T a)
{
a=a+(T)2;
}
#endif
__________________________
This just works fine but if you remove function body then like this:
Modified File 2: ADD2.h
__________________________
#ifndef ADD2_H
#define ADD2_H
template <class T>
T ADD2(T a);
#endif
__________________________
ADDED File 3: ADD2.cpp
__________________________
#include "ADD2.H"
template <class T>
T ADD2(T a)
{
a=a+(T)2;
}
__________________________
This Time again it will be compiled but a run time error like this can happen:
ZeroLink: unknown symbol '__Z4ADD2IiET_S0_'

It seems clear that we must define all the template in *.hpp??
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Template in *.h or *.cpp

 
0
  #4
Jun 30th, 2005
What's your compile command line?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: rkarimi is an unknown quantity at this point 
Solved Threads: 0
rkarimi rkarimi is offline Offline
Newbie Poster

Re: Template in *.h or *.cpp

 
0
  #5
Jun 30th, 2005
I use gcc 3.3++ on MAC or Xcode. But from concept of template it could be clear that it must be implemented in *.h, so on linking time required instances can be made?

Am I right?
:rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Template in *.h or *.cpp

 
0
  #6
Jun 30th, 2005
Put the implementation in an .hpp file and have it include the .h.

Then, in your main program file, include the .hpp file rather than the .h
Also, I think you want a reference in your ADD2 function (T & a), otherwise, I don't think 'a' will change... Also, you don't return anything in your function, you're supposed to return a T.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: rkarimi is an unknown quantity at this point 
Solved Threads: 0
rkarimi rkarimi is offline Offline
Newbie Poster

Re: Template in *.h or *.cpp

 
0
  #7
Jun 30th, 2005
Thanks,

This was just a test file.

BTW, putting the implementation in an *.hpp file is still in header file. I am not receiving my answer, how refering from an *.hpp file to *.h file can help me?

Thanks,Reza
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC