Same function implementation... different parameter types

Discussion in 'C++' started by Senthryl, Aug 24, 2006.

  1. Senthryl

    Senthryl Guest

    I just moved from Visual Basic to C++ and recently finished reading my
    book. However, I'm trying to implement something which has led me to
    writing the same thing many times, which I've learned is an indicator
    of poor design.

    I have a member function called GetInput of a class called UI. The
    function needs to accept all of the different built-in types as the
    same parameter. Therefore I decided to use overloading, but this
    requires that I type the body in for each overloaded version. The
    implementation for each function is the exact same, but I need the
    parameter to be able to accept all types.

    I've tried using templates, but I receive errors so I think that I'm
    using them incorrectly:
    class UI
    {
    public:
    //...
    template <class InType>
    void GetInput(InType& buffer) const;
    //...
    }
    //...
    template <class InType>
    void UI::GetInput(InType& buffer) const
    {
    //...
    }

    I get an error when linking code calling this function, saying that I
    have an unresolved external identifier. As far as I know that means
    it's looking for a overloaded version of the function.

    The way I'm currently using it is with macros, but I've been strongly
    advised against using them since they "are not type safe" and "are not
    built into the language."
    #define GETINPUT(InType) \
    void GetInput(InType& buffer) const \
    { //... }
    GETINPUT(unsigned short);GETINPUT(unsigned long); //...

    Is there a better way to do this than using macros? I'm using the
    Visual Studio 2003 compiler with service pack 1.
     
    Senthryl, Aug 24, 2006
    #1
    1. Advertising

  2. Senthryl wrote:
    > I just moved from Visual Basic to C++ and recently finished reading my
    > book. However, I'm trying to implement something which has led me to
    > writing the same thing many times, which I've learned is an indicator
    > of poor design.
    >
    > I have a member function called GetInput of a class called UI. The
    > function needs to accept all of the different built-in types as the
    > same parameter. Therefore I decided to use overloading, but this
    > requires that I type the body in for each overloaded version. The
    > implementation for each function is the exact same, but I need the
    > parameter to be able to accept all types.
    >
    > I've tried using templates, but I receive errors so I think that I'm
    > using them incorrectly:
    > class UI
    > {
    > public:
    > //...
    > template <class InType>
    > void GetInput(InType& buffer) const;
    > //...
    > }
    > //...
    > template <class InType>
    > void UI::GetInput(InType& buffer) const
    > {
    > //...
    > }
    >
    > I get an error when linking code calling this function, saying that I
    > have an unresolved external identifier. As far as I know that means
    > it's looking for a overloaded version of the function.


    This is answered in the FAQ:

    http://www.parashift.com/c -faq-lite/templates.html#faq-35.12

    Put the definition of the UI::GetInput template function in the header
    file that has the UI class definition, and your linker problems should
    disappear.

    Best regards,

    Tom
     
    Thomas Tutone, Aug 24, 2006
    #2
    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. Replies:
    3
    Views:
    888
  2. Christopher Brewster
    Replies:
    5
    Views:
    374
    John Machin
    Nov 14, 2008
  3. manu
    Replies:
    5
    Views:
    401
    Nick Keighley
    Sep 17, 2009
  4. bluebaron
    Replies:
    3
    Views:
    787
    Jonathan N. Little
    Nov 4, 2009
  5. Guest
    Replies:
    2
    Views:
    201
    Foo Man Chew
    Dec 29, 2003
Loading...

Share This Page