473,326 Members | 2,680 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,326 software developers and data experts.

template for class method problem

Below is a problem I am having with templates. I am trying to use a
template to avoid having multiple class methods that differ only in the
return type of the pointer. I have tried putting in another parm of the
typename to guarantee the uniqueness of the parm list but it didn’t
help. I am getting undefined externals from the linker, one for each
elemental type. Any ideas would be appreciated.

Thank you,

// Class declaration
class UTILITY_STUFF
{
public:

template<typename T>
T* GetJunk(unsigned int x, unsigned int y);
}
// Method usage
template<typename T>
T* UTILITY_STUFF::GetJunk(unsigned int x, unsigned int y)
{
return (T *)junkFunction(x,y);
}
// Template
template<typename T>
bool junk(T* pDummy, unsigned int X, unsigned int Y)
{
UTILITY_STUFF* someJunk;
nitfValue = someJunk->GetPixel<T>(X, Y);
}
Mar 3 '06 #1
3 1947
* Larry Beck:
Below is a problem I am having with templates. I am trying to use a
template to avoid having multiple class methods that differ only in the
return type of the pointer. I have tried putting in another parm of the
typename to guarantee the uniqueness of the parm list but it didn’t
help. I am getting undefined externals from the linker, one for each
elemental type.
That doesn't say much. But one common template-novice error is to put
template function definitions in an implementation file instead of the
header file. With current compilers a template function definition must
be available at the point where the function is called.

Any ideas would be appreciated.

Thank you,

// Class declaration
class UTILITY_STUFF
Don't use all UPPERCASE except for macro names.
{
public:

template<typename T>
T* GetJunk(unsigned int x, unsigned int y);
}
Missing semicolon.

// Method usage
template<typename T>
T* UTILITY_STUFF::GetJunk(unsigned int x, unsigned int y)
{
return (T *)junkFunction(x,y);
Don't use C-style casts. Also, this particular usage is extremely
dangerous because you're not only telling the compiler to accept the
code you have right here, no matter what. You're telling the compiler
to accept the _client_ code, no matter what type T it has supplied.
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 3 '06 #2
Alf P. Steinbach wrote:

That doesn't say much. But one common template-novice error is to put
template function definitions in an implementation file instead of the
header file. With current compilers a template function definition must
be available at the point where the function is called.

I have the the template function def in the header file. I just put
stripped down code in the message to make it easier to send.
// Method usage
template<typename T>
T* UTILITY_STUFF::GetJunk(unsigned int x, unsigned int y)
{
return (T *)junkFunction(x,y);


Don't use C-style casts. Also, this particular usage is extremely
dangerous because you're not only telling the compiler to accept the
code you have right here, no matter what. You're telling the compiler
to accept the _client_ code, no matter what type T it has supplied.
}


Can you give me a better way of doing it?

Thanks,
Mar 3 '06 #3
* Larry Beck:
Alf P. Steinbach wrote:

That doesn't say much. But one common template-novice error is to put
template function definitions in an implementation file instead of the
header file. With current compilers a template function definition
must be available at the point where the function is called.


I have the the template function def in the header file. I just put
stripped down code in the message to make it easier to send.


The best advice I can give: make a smallest possible program that
exhibits the problem, post the complete (smallest possible) code here.

// Method usage
template<typename T>
T* UTILITY_STUFF::GetJunk(unsigned int x, unsigned int y)
{
return (T *)junkFunction(x,y);


Don't use C-style casts. Also, this particular usage is extremely
dangerous because you're not only telling the compiler to accept the
code you have right here, no matter what. You're telling the compiler
to accept the _client_ code, no matter what type T it has supplied.
}


Can you give me a better way of doing it?


Depends what "it" is. One further problem with the code as shown is
that it doesn't tell what "it" is or could be... The only thing that
/seems/ clear is that the client code should be able to specify a type,
and in your implementation you must, somehow, make sure that the
specified type makes sense and is compatible with the result of
junkFunction -- but even this is perhaps not cast in stone, for
perhaps what you're trying to do does not require the client code to be
able to specify any type whatsoever?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 3 '06 #4

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

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
1
by: Adam Dziendziel | last post by:
Hi all! I'm writing a luabind/boost::python-like binding utility for a Squirrel language to generating wrapper-functions for C++ classes and have a problem with passing the pointer-to-member...
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
2
by: vilarneto | last post by:
Hello everyone, I'm facing a particular situation about template class derivation. The subject is old -- deriving a non-template class from a template base class -- but my problem is that the...
3
by: Adam Nielsen | last post by:
Hi everyone, Yet another syntax problem that's baffling me with templates. I want to instantiate a template with a single parameter as per normal, however the parameter is actually a template...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
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...
1
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.