473,799 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates and copy constructor

Hi,

following problem. I'm writing an Image class and want to use templates:

....
template<class TType, int TLayers>
class Image
{
public:
Image();

Image(std::stri ng filename);
....

Where TType stands for int, char aso and TLayers for Grey, RGB aso.

Now, converting Images from one type to another should be possible by
using the copy constructor:

Image<short, RGB> i("blub.jpg") ;
Image<double, Grey> j(i);

So, I'm trying to declare the copy contructor as follows:

template<class UType, int ULayers>
Image(const Image<UType, ULayers> &img);

and implement it:

template<class UType, int ULayers>
Image<unsigned char, Grey>::Image(co nst Image<UType, Ulayers> &img)
{
...
};
But at compile time I get the following error:
error: no `Image<unsigned char, 1>::Image(cons t Image<UType, ULayers>&)'
member function declared in class `Image<unsigned char, 1>'
error: invalid function declaration

Any ideas?

Cheers

matspi
Jul 23 '05 #1
4 3379
Matthias Spiller wrote:
Hi,

following problem. I'm writing an Image class and want to use templates:

...
template<class TType, int TLayers>
class Image
{
public:
Image();

Image(std::stri ng filename);
...

Where TType stands for int, char aso and TLayers for Grey, RGB aso.

Now, converting Images from one type to another should be possible by
using the copy constructor:

Image<short, RGB> i("blub.jpg") ;
Image<double, Grey> j(i);
This wouldn't use a copy constructor.
So, I'm trying to declare the copy contructor as follows:

template<class UType, int ULayers>
Image(const Image<UType, ULayers> &img);
It's not a copy constructor, but a conversion constructor from one template
instance to another.

and implement it:

template<class UType, int ULayers>
Image<unsigned char, Grey>::Image(co nst Image<UType, Ulayers> &img)
{
...
};


This is a specialization. Do you have a class definition for the
specialization Image<unsigned char, Grey>?

Maybe you wanted:

template<class TType, int TLayers>
template<class UType, int ULayers>
Image<TType, TLayers>::Image (const Image<UType, ULayers> &img)
{
//...
}

Jul 23 '05 #2



This is a specialization. Do you have a class definition for the
specialization Image<unsigned char, Grey>?

Maybe you wanted:

template<class TType, int TLayers>
template<class UType, int ULayers>
Image<TType, TLayers>::Image (const Image<UType, ULayers> &img)
{
//...
}


No, I don't have a definition for the specialization. How would this be
done?

I need a different constructor for each "destinatio n" class. So do I
have to define each class seperatly?

Image<unsigned char, Grey>
Image<short, Grey>
Image<float, Grey>
....
Thank you

Matthias
Jul 23 '05 #3
Matthias Spiller wrote:



This is a specialization. Do you have a class definition for the
specialization Image<unsigned char, Grey>?

Maybe you wanted:

template<class TType, int TLayers>
template<class UType, int ULayers>
Image<TType, TLayers>::Image (const Image<UType, ULayers> &img)
{
//...
}

No, I don't have a definition for the specialization. How would this be
done?


If you want to specialize a member of a template class, you have to
specialize the whole class. Something like:

template<>
class Image<unsigned char, Grey>
{
public:
template<class UType, int ULayers>
Image(const Image<UType, ULayers> &img);
//... the other members
};

template<>
template<class UType, int ULayers>
Image<unsigned char, int>::Image(con st Image<UType, ULayers> &img)
{
//...
}

I need a different constructor for each "destinatio n" class. So do I
have to define each class seperatly?
Yes. If you have common code, you can put it into a base class, or you can
write non-member functions, specialize them and call them from your class
members if you want to avoid specializing the class.
Image<unsigned char, Grey>
Image<short, Grey>
Image<float, Grey>
...
Thank you

Matthias


Jul 23 '05 #4
Me
> No, I don't have a definition for the specialization. How would this be
done?

I need a different constructor for each "destinatio n" class. So do I
have to define each class seperatly?

Image<unsigned char, Grey>
Image<short, Grey>
Image<float, Grey>


What you should do is define it once but use an adaptor function/class
(check out http://antigrain.com for a real-world example of how
somebody coded that up) to do all the work (I'm assuming you're copying
a 2d array of values from image to the other), like this (not sure if
this compiles, but you get the idea):

template<>
inline void convert<Grey, Grey>(unsigned char * &dest, const float *
&src)
{
*dest++ = clamp(0.0f, 255.0f, 255.0f * *src++);
}

template<>
inline void convert<Grey, RGB>(unsigned char * &dest, const float *
&src)
{
*dest++ = /*some formula involving src[0..2]*/;
src += 3;
}

etc.

template<class TType, int TLayers>
template<class UType, int ULayers>
Image<TType, TLayers>::Image (const Image<UTyle, ULayers> &img)
{
data = alloc(whatever) ;

for (size_t y = 0; y < height; ++y) {
TType *src = data(y);
UType *dest = img.data(y);
for (size_t x = 0; x < width; ++x) {
convert<TType, UType>(src, dest);
}
}
}

Personally, I wouldn't put that code directly in a constructor, I would
put it in a separate member function, that way it can be called at any
time. You can build a constructor that calls it if you want (but make
sure to mark it with 'explicit'). I'd recommend not making it a
constructor because it's hard to grep for and if you implement
copy-on-write to share images and change typedefs around later, code
that used to just do a refcount turns into an expensive conversion copy
that may throw an exception.

Jul 23 '05 #5

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

Similar topics

4
7175
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
1
2532
by: Tobias Langner | last post by:
I try to program a smart-pointer using policies. I rely heavyly on templates during this. The program compiles fine - but I get a linker error: test.o(.text+0x3e): In function `testSmartPtr()': /home/tobias/project/Heart/heartofoak/test.cpp:23: undefined reference to `SmartPtr<LongWrapper, RefCount>::SmartPtr(LongWrapper*)' collect2: ld returned 1 exit status I think the linker does not find the Constructor of the...
2
2176
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
1
3975
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
3
1928
by: Matt Bitten | last post by:
Hi, all. I have the same old problem about templates and copy constructors. I know this has been addressed hundreds of times, but despite perusing many old postings, and The Standard as well, I'm still feeling confused. Suppose I have a class template: template <typename T> class Foo {
1
1328
by: Greg | last post by:
Referring to Managed: why do templates require an explicit copy constructor to be defined in order to use gcnew whereas simply declaring an object as a local (still uses managed heap of course) does not require the template to have a default constructor defined? -- Greg McPherran www.McPherran.com
8
2881
by: NVH | last post by:
I know that this question may have been asked before but I can't find it here so: If there is a class: class Foo { ... Foo (const Foo &num); // Copy constructor template <typename T> Foo (const T &num); // Conversion constructor
8
429
by: Malciah | last post by:
I posted this on another site, but so far I've had no answers. So, I decided to try it here. -------------------------------------------------------- I've been learning C++ for about 6 weeks now, and I've been able to figure out the cause of pretty much every problem I've run into. This time, I'm at a loss. The book I'm learning from isn't the greatest in the world, and it hasn't given me a good explanation since chapter 6. (I'm in...
2
1906
by: saxman | last post by:
Hi everyone, I'm writing a class that inherits from std::vector in order to add some additional functionality. I'm having a compiler problem, however, when I have a function that returns this class. I've drilled down and been able to create a very simple example of the problem. Here's my code: <code> #include <vector>
0
9686
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
10475
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...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
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
10026
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
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
6805
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
5463
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...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.