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