473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me T>
T* GetJunk(unsigne d int x, unsigned int y);
}
// Method usage
template<typena me T>
T* UTILITY_STUFF:: GetJunk(unsigne d int x, unsigned int y)
{
return (T *)junkFunction( x,y);
}
// Template
template<typena me 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 1972
* 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<typena me T>
T* GetJunk(unsigne d int x, unsigned int y);
}
Missing semicolon.

// Method usage
template<typena me T>
T* UTILITY_STUFF:: GetJunk(unsigne d 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<typena me T>
T* UTILITY_STUFF:: GetJunk(unsigne d 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<typena me T>
T* UTILITY_STUFF:: GetJunk(unsigne d 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
11328
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 which is compiled and works fine. ############################################
11
3624
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: virtual void do_bar() {} };
2
2542
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 template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
0
1363
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 templates: ------------------------------------------ in file "Model.h":
1
4509
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 argument and deducing the type of them. I want to make a library which I can use like that: class_<fs::path, sq_filesystem::ttag>(v, "path", no_base)
3
4469
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 think this is really the issue. Here is a description of my problem. MathLibrary.dll: - SpaceTimeVector (uses various stuff from this dll)
1
3376
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 in EntityRepository template class. You can see the EntityRepository.h and EntityRepository.cpp and main method below: //EntityRepository.h #pragma once #ifndef ENTITYREPOSITORY_H #define ENTITYREPOSITORY_H
2
4236
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 base class has a pure virtual method. For instance: template<class T> class A { public:
3
1853
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 class itself, with all *its* parameters filled out (in the form of a typedef.) I can't work out how to break apart the typedef to reveal what data types were used to create it in the first place. Here is some example code that demonstrates the...
6
391
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
9568
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.