473,473 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Same function implementation... different parameter types

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.

Aug 24 '06 #1
1 2783
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-lit...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

Aug 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steph | last post by:
Hi, On my first oracle instance, I have a global type 'productarray' (CREATE OR REPLACE TYPE productarray AS TABLE OF VARCHAR(30)), I have a package procedure having myArray OUT productarray as...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
3
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
9
by: Ryan Taylor | last post by:
Hello. I am trying to overload a method so that I have four possible working copies. The only difference between the four methods is what to search by and in what manner to return the results....
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
9
by: Yannick | last post by:
Hi everyone - I am not quite sure to understand what is really going on when a function defined in one translation unit calls a function defined in a different translation unit without knowing...
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.