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

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::string 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(const Image<UType, Ulayers> &img)
{
...
};
But at compile time I get the following error:
error: no `Image<unsigned char, 1>::Image(const 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 3359
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::string 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(const 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 "destination" 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(const Image<UType, ULayers> &img)
{
//...
}

I need a different constructor for each "destination" 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 "destination" 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
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): ...
1
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()':...
2
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...
1
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...
3
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...
1
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)...
8
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...
8
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,...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.