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

Discussion in 'C++' started by Simon, May 2, 2006.

  1. Simon

    Simon Guest

    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.
    Simon, May 2, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, May 2, 2006
    #2
    1. Advertising

  3. Simon

    mlimber Guest

    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-lite/templates.html#faq-35.14).

    Cheers! --M
    mlimber, May 2, 2006
    #3
  4. 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.
    eastern_strider, May 2, 2006
    #4
  5. Simon

    mlimber Guest

    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
    mlimber, May 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DrUg13
    Replies:
    1
    Views:
    460
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    397
    Greg Comeau
    Nov 15, 2004
  3. christopher diggins
    Replies:
    16
    Views:
    738
    Pete Becker
    May 4, 2005
  4. Vinu
    Replies:
    9
    Views:
    594
  5. www.hitechskill.com
    Replies:
    0
    Views:
    1,327
    www.hitechskill.com
    Apr 9, 2006
Loading...

Share This Page