944,171 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2340
  • C RSS
Jun 30th, 2005
0

Template in *.h or *.cpp

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkarimi is offline Offline
8 posts
since Jun 2005
Jun 30th, 2005
0

Re: Template in *.h or *.cpp

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..
Reputation Points: 10
Solved Threads: 1
Light Poster
shre86 is offline Offline
33 posts
since Jun 2005
Jun 30th, 2005
0

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

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkarimi is offline Offline
8 posts
since Jun 2005
Jun 30th, 2005
0

Re: Template in *.h or *.cpp

What's your compile command line?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jun 30th, 2005
0

Re: Template in *.h or *.cpp

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkarimi is offline Offline
8 posts
since Jun 2005
Jun 30th, 2005
0

Re: Template in *.h or *.cpp

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.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jun 30th, 2005
0

Re: Template in *.h or *.cpp

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkarimi is offline Offline
8 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help me in Round Robin Algorithem& Shortest remaining time next
Next Thread in C Forum Timeline: program error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC