Same function implementation... different parameter types

S

Senthryl

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.
 
T

Thomas Tutone

Senthryl said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top