472,360 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 2567
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Math Preregistration System | last post by:
I'm using a std::list as a container for some pointers to objects, for example list< C* > lst; I would like to sort them using two different criteria, say first by C.first and then by...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
5
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T> class matrix {
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
10
by: jason.cipriani | last post by:
Is there any difference between declaring a template parameter as a "typename" or a "class"? E.g. template <class TT f() { } template <typename TT g() { } Thanks, Jason
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.