473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help on templates


I would like to find out how I can effectively make use of templates in
theexample below.

In Class A, I have 3 overloaded member functions "send" for handling
the different messages. Although the messages are different, the way to
handle the message is the same. Hence I'm thinking of using templates
to reduce the number of lines in the code.

------ ClassA.h ------
struct Message1;
struct Message2;
struct Message3;

class A
{
public:
.....
void send(Messages1);
void send(Messages2);
void send(Messages3);
};
------------------------------
I have a calling class that will call the send method in A.
------- CallingClass.cpp ---------
Struct Message1 msg1;
msg1.set(....);
mClassA->send(msg1);
-------------------------------------------

The above works but I wonder if i can use templates to reduce the lines
of codes.

In an attempt to use templates, what I did was:
----- ClassA.h --------------------
class A
{
public:
template <class S> void send(S);
}
--------------------

the calling class remains the same ...

------- CallingClass.cpp ---------
Struct Message1 msg1;
msg1.set(....);
mClassA->send(msg1);
-----------------------------------------------

I was able to compile with the codes above, but I have linker error. It
is unable to locate the implementation of send (Message1) in class A.

I would like to find out how to implement the above using templates.
I'm sorry if I am unclear.

Many thanks in advance!

Regards,
Janice

Nov 22 '05 #1
4 1474

qn*****@hotmail.com wrote:

In an attempt to use templates, what I did was:
----- ClassA.h --------------------
class A
{
public:
template <class S> void send(S); This means that send is a template function. In the current context, S
could be Message1, Message2 etc.

I would like to find out how to implement the above using templates.


template <class S> void send(S msg)
{
// write your code as usual here, and use S wherever you used
Message1 etc in your original code. (There are some more issues but
they could be pointed out if the code is made available.)

}

Hope this helps.

Nov 22 '05 #2
Neelesh Bodas wrote:
qn*****@hotmail.com wrote:

In an attempt to use templates, what I did was:
----- ClassA.h --------------------
class A
{
public:
template <class S> void send(S);

This means that send is a template function. In the current context, S
could be Message1, Message2 etc.

I would like to find out how to implement the above using templates.


template <class S> void send(S msg)
{
// write your code as usual here, and use S wherever you used
Message1 etc in your original code. (There are some more issues but
they could be pointed out if the code is made available.)

}

Hope this helps.


Depending on your design, you might prefer to use the dynamic
polymorphism of inheritance rather than the static polymorphism of
templates. In particular, you could send messages by a base class with
a standard interface, e.g.,

struct Msg
{
virtual std::string Get() const = 0;
};

struct Msg1 : Msg
{
void Set( /* ... */ ) { /*...*/ }
std::string Get() const { /* ... */ }
};

struct Msg2 : Msg
{
void Set( /* ... */ ) { /*...*/ }
std::string Get() const { /* ... */ }
};

struct A
{
void Send( const Msg& msg )
{
std::cout << msg.Get() << std::endl;
}
};

struct B
{
template<class M>
void Send( const M& msg )
{
std::cout << msg.Get() << std::endl;
}
};
void Foo()
{
Msg1 m1;
Msg2 m2;

m1.Set( /*...*/ );
m2.Set( /*...*/ );

A a;
a.Send( m1 );
a.Send( m2 );

B b;
b.Send( m1 );
b.Send( m2 );
}

Class A implements dynamic polymorphism and class B static polymorphism
(technically, the inheritance specs on Msg1 and Msg2 should be deleted
for static polymorphism, but you get the idea). A tends to be more
efficient in terms of code size, especially if there are many Msg
classes, and B tends to be *slightly* more efficient in terms of
execution speed. I would prefer the first way (class A) if possible.

Cheers! --M

Nov 22 '05 #3

mlimber:
I can't use dynamic polymorphism of inheritance as the messages are
very different in terms of structure and they are legacy codes that I
can't change. I have to make do with Message1, Message2, Message3 and
so on.

Neelesh:
I did just that with send as the member function of Class A. I make use
of S in place of Message 1 etc.

I have a problem in linking the codes. In the calling class, I make a
call like this:

Message1 msg1;
mClassA->send(msg1);

The linker fails to locate the object implementation of
ClassA::send(Message1). It fails to link with the template.

Nov 22 '05 #4
> I have a problem in linking the codes. In the calling class, I make a
call like this:

Message1 msg1;
mClassA->send(msg1);

The linker fails to locate the object implementation of
ClassA::send(Message1). It fails to link with the template.


Unless templates are "export"ed, the definition of templatized function
must be made available at compile time. Since the template code is
instantiated at compile time, hence the liker doesnot come in the
picture here.
It would be better if you could actually show the code which is causing
the error.

Nov 22 '05 #5

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

Similar topics

6
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
6
by: Virginia Kirkendall | last post by:
Hi: I'm new with this & need help creating a XSL table that looks like the following: --------------------------------------------------------- | | | | | |...
12
by: Amanda | last post by:
I have tried everything with this! I get an error stating "Index was outside the bounds of the array" My code looks like this.... xmlDoc = New XmlDocument() xmlDoc.Load("xml.doc") xslDoc...
4
by: Piper707 | last post by:
I need help with using a general template which would process all tags other than the ones for which specific templates have been written. My XML looks like this: <ITEMS>...
1
by: oldgent | last post by:
I am having a problem installing the starter kits. I have reinstalled VS 2005, think that might be the problem. I then installed both 'Personal Website" and the "Club Website" starter kits. I...
2
by: shapper | last post by:
Hello, I am for days trying to apply a XSL transformation to a XML file and display the result in a the browser. I am using Asp.Net 2.0. Please, could someone post just a simple code example,...
1
by: kaye80 | last post by:
I have an XML and XSL document. I am trying to incorporate hyperlinks throughout the XML document. I have read many of the related posts on this and other forums but I can't seem to get it right. ...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
2
by: erikcw | last post by:
Hi all, I'm trying to write a loop that will build a list of "template strings". My current implementation is *really slow*. It took 15 minutes to finish. (final len(list) was about 16k...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.