473,394 Members | 1,817 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.

Template class copy constructor

I have declared a copy constructor for a template class in a Visual C++
project but for some reason the compiler ignores it. I can put syntax
errors in the copy constructor and the compiler ignores them.

So what's the problem with my code?

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;
typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{
public:

inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{
m_3DVector = new pixel_data[nRow*nCol*nZed];
}
explicit inline C3DVector(const C3DVector<pixel_data>& rhs)
{
// we're assuming they're the same size
if (this != &rhs)
{
delete[] m_3DVector;
rhs.getdimensions(m_dimRow, m_dimCol);
m_dimZed = 3;
m_3DVector = new pixel_data[m_dimRow*m_dimCol*m_dimZed];
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=rhs(x,y,z); //
replaced indirection with direct access!
}
}
inline ~C3DVector()
{
delete[] m_3DVector;
}

inline C3DVector<pixel_data>& operator= (C3DVector<pixel_data>&
second)
{
// we're assuming they're the same size
if (this != &second)
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=second(x,y,z); //
replaced
return(*this);
}
inline pixel_data& operator()(int x, int y, int z)
{
if (x<0 || x>m_dimRow ||
y<0 || y>m_dimCol ||
z<0 || z>m_dimZed)
throw("Access 3D vector out of bounds");
return(m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]);
}

void writegreybmp(int colourcomponent,
CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);

void writecolourbmp(CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);

bool capturecolourbmp(CString m_csInputFilename);
inline void getdimensions(unsigned int row, unsigned int col)
{
row = m_dimRow;
col = m_dimCol;
}
protected:
pixel_data* m_3DVector;

unsigned int m_dimRow;
unsigned int m_dimCol;
unsigned int m_dimZed;
static const int RED=0, GREEN=1, BLUE=2;
};

#endif
Thanks in advance
-Muz

Mar 10 '06 #1
9 2720

mu*****@gmail.com wrote:
I have declared a copy constructor for a template class in a Visual C++
project but for some reason the compiler ignores it. I can put syntax
errors in the copy constructor and the compiler ignores them.

So what's the problem with my code?

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;
typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{
public:

inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{
m_3DVector = new pixel_data[nRow*nCol*nZed];
}
explicit inline C3DVector(const C3DVector<pixel_data>& rhs)
{
// we're assuming they're the same size
if (this != &rhs)
{
delete[] m_3DVector;
rhs.getdimensions(m_dimRow, m_dimCol);
m_dimZed = 3;
m_3DVector = new pixel_data[m_dimRow*m_dimCol*m_dimZed];
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=rhs(x,y,z); //
replaced indirection with direct access!
}
}
inline ~C3DVector()
{
delete[] m_3DVector;
}

inline C3DVector<pixel_data>& operator= (C3DVector<pixel_data>&
second)
{
// we're assuming they're the same size
if (this != &second)
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=second(x,y,z); //
replaced
return(*this);
}
inline pixel_data& operator()(int x, int y, int z)
{
if (x<0 || x>m_dimRow ||
y<0 || y>m_dimCol ||
z<0 || z>m_dimZed)
throw("Access 3D vector out of bounds");
return(m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]);
}

void writegreybmp(int colourcomponent,
CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);

void writecolourbmp(CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);

bool capturecolourbmp(CString m_csInputFilename);
inline void getdimensions(unsigned int row, unsigned int col)
{
row = m_dimRow;
col = m_dimCol;
}
protected:
pixel_data* m_3DVector;

unsigned int m_dimRow;
unsigned int m_dimCol;
unsigned int m_dimZed;
static const int RED=0, GREEN=1, BLUE=2;
};

#endif
Thanks in advance
-Muz


Are you sure you are calling it correctly ? Copy constructor looks ok
as far as the signature is concerned.

Also, you are calling too non-const functions(getdimensions and
operator()) on the const rhs object in the copy constructor. That will
give you compiler errors. Make them constant and the code should
compile and the copy constructor should get called.

Mar 10 '06 #2

mu*****@gmail.com wrote:
I have declared a copy constructor for a template class in a Visual C++
project but for some reason the compiler ignores it. I can put syntax
errors in the copy constructor and the compiler ignores them.

So what's the problem with my code?

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;
typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{
public:

inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{
m_3DVector = new pixel_data[nRow*nCol*nZed];
}
explicit inline C3DVector(const C3DVector<pixel_data>& rhs)
{
// we're assuming they're the same size
if (this != &rhs)
{
delete[] m_3DVector;
rhs.getdimensions(m_dimRow, m_dimCol);
m_dimZed = 3;
m_3DVector = new pixel_data[m_dimRow*m_dimCol*m_dimZed];
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=rhs(x,y,z); //
replaced indirection with direct access!
}
}
inline ~C3DVector()
{
delete[] m_3DVector;
}

inline C3DVector<pixel_data>& operator= (C3DVector<pixel_data>&
second)
{
// we're assuming they're the same size
if (this != &second)
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=second(x,y,z); //
replaced
return(*this);
}
inline pixel_data& operator()(int x, int y, int z)
{
if (x<0 || x>m_dimRow ||
y<0 || y>m_dimCol ||
z<0 || z>m_dimZed)
throw("Access 3D vector out of bounds");
return(m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]);
}

void writegreybmp(int colourcomponent,
CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);

void writecolourbmp(CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);

bool capturecolourbmp(CString m_csInputFilename);
inline void getdimensions(unsigned int row, unsigned int col)
{
row = m_dimRow;
col = m_dimCol;
}
protected:
pixel_data* m_3DVector;

unsigned int m_dimRow;
unsigned int m_dimCol;
unsigned int m_dimZed;
static const int RED=0, GREEN=1, BLUE=2;
};

#endif
Thanks in advance
-Muz


Are you sure you are calling it correctly ? Copy constructor looks ok
as far as the signature is concerned.

Also, you are calling too non-const functions(getdimensions and
operator()) on the const rhs object in the copy constructor. That will
give you compiler errors. Make them constant and the code should
compile and the copy constructor should get called.

Mar 10 '06 #3
In the program an object of the C3DVector class is added to an stl
deque so i think the copy constructor should be called to put a copy in
the deque.

The program crashes around this point and Visual c++'s call stack shows
that a C3DVector copy constructor is called but it isn't the one I
wrote. Visual c++ says "there is no source code available for that
location" when I click on it, I think that means it's generating its
own.
SD2.exe!C3DVector<unsigned char>::C3DVector<unsigned char>(const C3DVector<unsigned char> & __that={...}) + 0x2f C++

I have made some template specialization functions for unsigned char
but not a copy constructor for unsigned char.

Also, I should be clear on this. The program and the C3DVector class
compile even when I take out all the code in the copy constructor and
put gibberish in there. That doesn't happen with the other functions.

Mar 10 '06 #4

muzm...@gmail.com wrote:
In the program an object of the C3DVector class is added to an stl
deque so i think the copy constructor should be called to put a copy in
the deque.

The program crashes around this point and Visual c++'s call stack shows
that a C3DVector copy constructor is called but it isn't the one I
wrote. Visual c++ says "there is no source code available for that
location" when I click on it, I think that means it's generating its
own.
SD2.exe!C3DVector<unsigned char>::C3DVector<unsigned char>(const C3DVector<unsigned char> & __that={...}) + 0x2f C++ I have made some template specialization functions for unsigned char
but not a copy constructor for unsigned char.


I think I have seen issues debugging templates(and even some cirtual
functions) with VC++ debugger where you cannot trace in into those
functions at times if you dont have all the debugging options set.
Probably that is why it says there is no source code and then you have
to go past. Are you sure you have all the debugging options set
correctly ? Both in the C/C++ options and Linker options.

Also post the entire code, because the code you posted didnt show any
specialization. If you cant post your real code, try making a test code
and post it here to demonstrate the problem.

Also, I should be clear on this. The program and the C3DVector class
compile even when I take out all the code in the copy constructor and
put gibberish in there. That doesn't happen with the other functions.


Again if you are using specialization, post all the code here. Based on
what you are describing, you dont have a copy constructor in the
specialized code( and the compiler might be generating that for you ??)

Mar 10 '06 #5
Alright, here's a condensed version of the C3DVector class which may
allow you to help.

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{public:
inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{m_3DVector = new pixel_data[nRow*nCol*nZed];}
template<class pixel_data> C3DVector(const C3DVector<pixel_data>& rhs)
{
// code never reaches this point
}
//other functions
protected:
pixel_data* m_3DVector; unsigned int m_dimRow; unsigned int
m_dimCol; unsigned int m_dimZed; static const int RED=0, GREEN=1,
BLUE=2;
};
#endif
And here's the throughly unremarkable template specialization header
file (the problem better not be here).
#ifndef threedeeBYTEmatrix_h
#define threedeeBYTEmatrix_h
#include <vector>
template<>
void C3DVector<BYTE>::
writegreybmp(int colourcomponent,
CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);
template<>
void C3DVector<BYTE>::
writecolourbmp(CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);
template<>
bool C3DVector<BYTE>::
capturecolourbmp(CString m_csInputFilename);
#endif

Mar 10 '06 #6

mu*****@gmail.com wrote:
Alright, here's a condensed version of the C3DVector class which may
allow you to help.

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{public:
inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{m_3DVector = new pixel_data[nRow*nCol*nZed];}
template<class pixel_data> C3DVector(const C3DVector<pixel_data>& rhs)
{
// code never reaches this point
}
//other functions
protected:
pixel_data* m_3DVector; unsigned int m_dimRow; unsigned int
m_dimCol; unsigned int m_dimZed; static const int RED=0, GREEN=1,
BLUE=2;
};
#endif
And here's the throughly unremarkable template specialization header
file (the problem better not be here).
#ifndef threedeeBYTEmatrix_h
#define threedeeBYTEmatrix_h
#include <vector>
template<>
void C3DVector<BYTE>::
writegreybmp(int colourcomponent,
CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);
template<>
void C3DVector<BYTE>::
writecolourbmp(CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);
template<>
bool C3DVector<BYTE>::
capturecolourbmp(CString m_csInputFilename);
#endif


The code that I see here is that you have specialized the member
functions ? I am not sure if this code will even compile.
First you need to specialize your class C3DVector for BYTE( maybe you
havent posted that code ) and then implement those member functions for
that specialized class. Going by your declarations and defintions of
member functions, they dont seemed to be member templates.

Show how you are specializing the C3DVector class. Does it have a copy
constructor ? Because if you are 'explicity" instantiating the
specialized C3DVector, the copy constructor in the non-spec C3DVector
has nothing to do with it and that is why it is not getting called.
It is not inheritance, if at all that is what you are thinking.

Mar 10 '06 #7
mu*****@gmail.com wrote:
I have declared a copy constructor for a template class in a Visual C++
project but for some reason the compiler ignores it. I can put syntax
errors in the copy constructor and the compiler ignores them.

So what's the problem with my code?

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;
typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{
public:

inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{
m_3DVector = new pixel_data[nRow*nCol*nZed];
}
explicit inline C3DVector(const C3DVector<pixel_data>& rhs)
{
// we're assuming they're the same size
if (this != &rhs)
{
delete[] m_3DVector;
rhs.getdimensions(m_dimRow, m_dimCol);
m_dimZed = 3;
m_3DVector = new pixel_data[m_dimRow*m_dimCol*m_dimZed];
for (unsigned int x=0; x<m_dimRow; x++)
for (unsigned int y=0; y<m_dimCol; y++)
for (unsigned int z=0; z<m_dimZed; z++)
m_3DVector[x*m_dimCol*m_dimZed+y*m_dimZed+z]=rhs(x,y,z); //
replaced indirection with direct access!
}
}


Is there a reason your copy constructor is explicit?

Also, what version of VC are you using? VC6 (and VC7 aka 2002) is
notorious for poor template support. VC7.1 aka 2003 is fairly Standard
compliant.
Mar 10 '06 #8
Besides from other things, your copy constructor declaration/definition is
incorrect.
You should write simply:

C3DVector(const C3DVector & rhs)
{
// do your copy here
}

<mu*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Alright, here's a condensed version of the C3DVector class which may
allow you to help.

#ifndef threedeematrix_h
#define threedeematrix_h

#include <vector>

using namespace std;typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{public:
inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0){;}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed)
{m_3DVector = new pixel_data[nRow*nCol*nZed];}
template<class pixel_data> C3DVector(const C3DVector<pixel_data>& rhs)
{
// code never reaches this point
}
//other functions
protected:
pixel_data* m_3DVector; unsigned int m_dimRow; unsigned int
m_dimCol; unsigned int m_dimZed; static const int RED=0, GREEN=1,
BLUE=2;
};
#endif
And here's the throughly unremarkable template specialization header
file (the problem better not be here).
#ifndef threedeeBYTEmatrix_h
#define threedeeBYTEmatrix_h
#include <vector>
template<>
void C3DVector<BYTE>::
writegreybmp(int colourcomponent,
CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);
template<>
void C3DVector<BYTE>::
writecolourbmp(CString outputfilename,
BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih,
bool popupbox);
template<>
bool C3DVector<BYTE>::
capturecolourbmp(CString m_csInputFilename);
#endif

Mar 11 '06 #9
So, I left work on Friday, vowing to make a post about the resolution
to my problem on Monday. On Monday I busy doing other stuff so I
finally got a chance to respond on Tuesday, and I can't remember how
the problem was solved.
The program compiles now and calls the copy constructor when necessary
so I can post an abridged copy of the code but I can't say exactly how
I fixed it.

I'd just like to thank everybody for their suggestions and help in C++
code. I really appreciate it.

-Muz

#ifndef threedeematrix_h
#define threedeematrix_h

using namespace std;
typedef unsigned char BYTE;

template <class pixel_data>
class C3DVector
{
public:

inline C3DVector():m_dimRow(0), m_dimCol(0), m_dimZed(0),
m_3DVector(NULL){}
inline C3DVector(int nRow, int nCol, int
nZed):m_dimRow(nRow),m_dimCol(nCol),m_dimZed(nZed) , m_3DVector(new
pixel_data[nRow*nCol*nZed])
{}

inline C3DVector(const C3DVector<pixel_data>& source)
{
copyelements(source);
}

~C3DVector();
C3DVector<pixel_data>& operator= (C3DVector<pixel_data>& source);
void copyelements(const C3DVector<pixel_data>& source);
pixel_data& operator() (int x, int y, int z) const;
void getdimensions (unsigned int& row, unsigned int& col, unsigned
int& zed)const;

// the following functions are only defined for C3DVector<BYTE>
void writegreybmp(int colourcomponent, CString outputfilename,
BITMAPFILEHEADER bfh, BITMAPINFOHEADER bih, bool popupbox);
void writecolourbmp(CString outputfilename, BITMAPFILEHEADER bfh,
BITMAPINFOHEADER bih, bool popupbox);
bool capturecolourbmp(CString m_csInputFilename);

protected:
unsigned int m_dimRow;
unsigned int m_dimCol;
unsigned int m_dimZed;
pixel_data* m_3DVector;
static const int RED=0, GREEN=1, BLUE=2;
};
#endif

Mar 14 '06 #10

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

Similar topics

6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
3
by: jack | last post by:
Hi there, I have a function F(x, y, z) and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0) z0 is fixed. Suppose somebody wrote a routine called "Compute" which simply computes f(a)...
2
by: Indrawati Yahya | last post by:
Hi I am having trouble in defining a templated class' copy constructor. Here is the simplest code that represent my problem: template<typename T1> class Foo { public: Foo(T1 bar = T1()):...
3
by: yomgui | last post by:
Hi, The following code compiles on Unix but refuse to do so with devstudio6 can anyone help me to get it right ? (ie for visual studio) template <class T> class AutoPtr { public:
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
3
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain...
13
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
4
by: Deep | last post by:
Can I use a class in this manner, where a constructor is of templated: template<typename T> class my_class{ public: int x_; public: template<typename U> my_class(const my_class<U>& other ) :...
3
by: Fred Kleinschmidt | last post by:
I have a template:: template <class Vclass Vct { public: Vct(V); }; template <class V> Vct<V>::Vct( V vin ) { /*...*/
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
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
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...

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.