473,327 Members | 1,967 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,327 software developers and data experts.

Yet another template question :o(

Hi,

before I gouge my eyes out with a teaspoon, hopefully someone will be
able to shed some light on the black art of templates!

I've exhausted the search engine, FAQs, groups etc looking for an answer
but it seems that getting the right search phrase is also a bit of an
art ;o)

OK, so here is what I've got (simplified):

/*storage for our sonar message*/
template <class T>
class CSonarMsg
{
public:
CSonarMsg(){}
~CSonarMsg(){}

SonarMessageHeaderType m_hSonarHdr;
T m_hSonarData;

void getMsg(SonarMessageHeaderType& sonarHdr, T& sonarData)
{
memcpy(&sonarHdr, &m_hSonarHdr, sizeof(SonarMessageHeaderType));
memcpy(&sonarData, &m_hSonarData, sizeof(T));
}
};

/*driver class*/
class CETSSCtrl
{
public:
CETSSCtrl(){}
~CETSSCtrl(){}

CSonarMsg<SonarMessageStatusTypem_hSonarMsgStatus;
CSonarMsg<SonarMessagePingTypem_hSonarMsgPing;

template <typename T>
void getMsg(unsigned char msgType, SonarMessageHeaderType& sonarHdr,
T& sonarData);
}

and in the CETSSCtrl.cpp I have:

template <typename T>
void CETSSCtrl::getMsg(unsigned char msgType, SonarMessageHeaderType&
sonarHdr, T& sonarData)
{
switch(msgType)
{
case STATUS_TYPE:
m_hSonarMsgStatus.getMsg(sonarHdr, sonarData);
break;

case PING_TYPE:
m_hSonarMsgPing.getMsg(sonarHdr, sonarData);
break;
}
}
Now the GUI thread calls the driver classes getMsg function with the
correct parameters. This is where I got my first headache. I didn't know
much about templates and hence about the fact that you need to have the
code inline ( I also read that if you don't want to have the code in
your header you can declare the function prototype for each type you
will use at the top of the cpp file. I couldn't get this to work - maybe
down to my syntax so maybe someone can tell me how it should be written?)

Anyway, I put the code inline and I get a compile error in the switch
statement. If I call the getMsg function with the
SonarMessageStatusType type then it complains (rightly so) that it
cannot convert from SonarMessageStatusType to SonarMessagePingType in
the second case of the switch statement (even though it won't get into
there). I understand that it is complaining because I am using two types
of instantiated objects in this template function but only passing in
one type.

My Questions:

1) Is my code structure flawed (or to put it bluntly crap!)
2) should i should a <reinterpret_castfor each case in the switch so
that each object is forced to accept the type that was defined for it?
3) Should I just ditch the whole getMsg template and use a normal
function with void pointers?

Any help greatly appreciated.

Regards,

John.

[NOTE: I've not supplied my real email address as I prefer to keep all
replies to the newsgroup so that everyone can share in the knowledge!]
Oct 27 '06 #1
1 1181
John wrote:
Hi,

before I gouge my eyes out with a teaspoon, hopefully someone will be
able to shed some light on the black art of templates!

I've exhausted the search engine, FAQs, groups etc looking for an answer
but it seems that getting the right search phrase is also a bit of an
art ;o)

OK, so here is what I've got (simplified):

/*storage for our sonar message*/
template <class T>
class CSonarMsg
{
public:
CSonarMsg(){}
~CSonarMsg(){}

SonarMessageHeaderType m_hSonarHdr;
T m_hSonarData;

void getMsg(SonarMessageHeaderType& sonarHdr, T& sonarData)
{
memcpy(&sonarHdr, &m_hSonarHdr,
sizeof(SonarMessageHeaderType));
memcpy(&sonarData, &m_hSonarData, sizeof(T));
}
};

/*driver class*/
class CETSSCtrl
{
public:
CETSSCtrl(){}
~CETSSCtrl(){}

CSonarMsg<SonarMessageStatusTypem_hSonarMsgStatus;
CSonarMsg<SonarMessagePingTypem_hSonarMsgPing;

template <typename T>
void getMsg(unsigned char msgType, SonarMessageHeaderType&
sonarHdr, T& sonarData);
}

and in the CETSSCtrl.cpp I have:

template <typename T>
void CETSSCtrl::getMsg(unsigned char msgType, SonarMessageHeaderType&
sonarHdr, T& sonarData)
{
switch(msgType)
{
case STATUS_TYPE:
m_hSonarMsgStatus.getMsg(sonarHdr, sonarData);
break;

case PING_TYPE:
m_hSonarMsgPing.getMsg(sonarHdr, sonarData);
break;
}
}
Now the GUI thread calls the driver classes getMsg function with the
correct parameters. This is where I got my first headache. I didn't know
much about templates and hence about the fact that you need to have the
code inline ( I also read that if you don't want to have the code in
your header you can declare the function prototype for each type you
will use at the top of the cpp file. I couldn't get this to work - maybe
down to my syntax so maybe someone can tell me how it should be written?)
Declare all your template function bodies in the header.
Anyway, I put the code inline and I get a compile error in the switch
statement.
What error?

See if you can pare it down to the minimum compilable (or in your case,
non-compilable with the error) example. And post the full thing.
[redacted]
My Questions:

1) Is my code structure flawed (or to put it bluntly crap!)
We don't know, you didn't post enough.
2) should i should a <reinterpret_castfor each case in the switch so
that each object is forced to accept the type that was defined for it?
No.
3) Should I just ditch the whole getMsg template and use a normal
function with void pointers?
No! You want the type safety you get with templates.
Oct 27 '06 #2

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

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.