473,549 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.cp p ---------
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.cp p ---------
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 1487

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(Me ssage1). 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(Me ssage1). 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
2726
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 straight XML file http://www.discovertravelandtours.com/test/templates/test.xml?Location=Germany To another XML file ...
6
3666
by: Virginia Kirkendall | last post by:
Hi: I'm new with this & need help creating a XSL table that looks like the following: --------------------------------------------------------- | | | | | | |Title |CrossCut |Institution |START |END | | | | | | | | | |PI | | | | | | | | |
12
2122
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 = New XslTransform()
4
1578
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> <SPECIAL1>abc</SPECIAL1> <SPECIAL2>abc</SPECIAL2> ... <SPECIAL12>abc</SPECIAL9>
1
1594
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 still have the same problem. Any web starter kit that is installed under 'My Templates" does not allow you to enter the location directory or use the...
2
1960
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, either C# or VB.NET, that I can try. Thank You Very Much,
1
1650
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. Please help! Do I have to declare the namespace in the XSL doc also as shown below or should I take it out? No matter what I do, nothing appears...
4
2762
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 (fromStop) and its associated driving directions back to a relational database. I have asked my software vendor for solutions but thus far they have not come...
5
3146
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 problem is exactly. I am hoping its an easy overlook. This class is of the linkedLIst, Iterator, and Node modified to use Templates and so that...
2
963
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 entries.) #combinations = 12 small template strings ie "{{ city }}, {{ state }}..."
0
7524
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6048
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.