469,140 Members | 1,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

template<class T> in the h file and cpp file?

Hi,

I am trying to create a round kind of function, (to round float and double
to int).
In the H file I can do the following with not too much trouble...

//
#include <limits>
#include <math.h>

template<class T>
int round( T x )
{
if(x > std::numeric_limits<int>::max()){
return 0;
}

return (int)(x + (x > 0 ? 0.5 : -0.5));
}

//
//

But how can I define it in the h file and then implement it in the cpp file?

//
// myfile.h
template<class T> int round( T x );

//
// myfile.cpp
template<class T>
int round( T x )
{
// ..
}

And how's my round(...) function looking?

Simon
--
http://urlkick.com/
Free URL redirection service. Turns a long URL into a much shorter one.
May 2 '06 #1
4 2405
Simon wrote:
I am trying to create a round kind of function, (to round float and
double to int).
In the H file I can do the following with not too much trouble...

//
#include <limits>
#include <math.h>

template<class T>
int round( T x )
{
if(x > std::numeric_limits<int>::max()){
return 0;
}

return (int)(x + (x > 0 ? 0.5 : -0.5));
}

//
//

But how can I define it in the h file and then implement it in the
cpp file?
[..]
This is covered in the FAQ. Please see the section on templates.

And how's my round(...) function looking?


OK. You know, if it does what you need it to do, why do you care how
it looks?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 2 '06 #2
Simon wrote:
Hi,

I am trying to create a round kind of function, (to round float and double
to int).
Consider using Boost's Numeric Conversion Library
(http://boost.org/libs/numeric/conversion/) instead of rolling your
own.
In the H file I can do the following with not too much trouble...

//
#include <limits>
#include <math.h>
You don't use math.h, but if you did, you should probably prefer
<cmath>.

template<class T>
int round( T x )
{
if(x > std::numeric_limits<int>::max()){
What if x < std::numeric_limits<int>::min()?
return 0;
Is returning an incorrect value better than throwing a range exception?
Maybe depending on your circumstances, but I'd probably prefer the
latter.
}

return (int)(x + (x > 0 ? 0.5 : -0.5));
}

//
//

But how can I define it in the h file and then implement it in the cpp file?

[snip]

Use the export keyword if your compiler supports it (most don't; see
http://www.parashift.com/c++-faq-lit...tml#faq-35.14).

Cheers! --M

May 2 '06 #3
I think you can't.

There might be ways to define the functions of a static class in a .cpp
file, but I believe according to the standards both the declaration and
the definition should be in a .h file.

May 2 '06 #4
eastern_strider wrote:
I think you can't.

There might be ways to define the functions of a static class in a .cpp
file, but I believe according to the standards both the declaration and
the definition should be in a .h file.


No. According to the Standard, you can use the export keyword. It's
just that few compilers support (or plan to support) the export
keyword. See the aforementioned FAQ.

Cheers! --M

May 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Math Preregistration System | last post: by
5 posts views Thread by tuko | last post: by
14 posts views Thread by SoilMan | last post: by
10 posts views Thread by jason.cipriani | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.