M
muzmail
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
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