Hey guys,
Ive heard just praises about this place, but never had the opportunity to give it a shot, till today... I got this code to read a bmp file in c++, and it uses templates. Ive tried just about everything to create an object of that class, but reached nowhere. Ive compiled just the header file and it works fine, but like I said before it seems impossible to create an object of that class. The header file is as follows:
Thanks a million
ashirazi
***********************************************************
#ifndef _IMAGE_CLASS
#define _IMAGE_CLASS
#include<string.h> // for memcpy();
#include<assert.h> // for assert();
#include<fstream.h>
const int STRING_SIZE = 512;
typedef char String[STRING_SIZE + 1];
//end sudeep
template <class T> class Image
{
public:
// constructor and destructor
Image();
Image( int nRows, int nCols);
Image( Image& m);
~Image();
// operations
void Initialize( T x=0);
int ResetSize(int rows, int cols);
// operator overload
operator T** (){ return m_ptr; }
void operator=(const Image& m);
// data access
T** GetBuffer(){ return m_ptr; }
int Rows(){ return m_nRows; }
int Cols(){ return m_nCols; }
T& CellAt(int col, int row); // safe access
int Read( const char* fn); //read from fn according to m_nRows & m_nCols
int Write( const char* fn);
protected:
void FreeMem();
private:
T** m_ptr; // the 3-D pointer.
int m_nRows;
int m_nCols;
};
#endif
***********************************************************
#include"image.h"
// implementation
// constructors and destructor
template <class T>
Image<T>::Image():m_nRows(0),m_nCols(0),m_ptr(NULL)
{
}
template <class T>
Image<T>::Image(int rows, int cols):m_nRows(rows),m_nCols(cols)
{
m_ptr=NULL;
int bSuc=ResetSize(rows, cols);
}
template <class T>
Image<T>::Image(Image& m): m_nRows(m.m_nRows),m_nCols(m.m_nCols),m_ptr(NULL)
{
int bSuc=ResetSize(m_nRows,m_nCols);
m_ptr = new T*[m_nRows];
assert(m_ptr != NULL);
for(int row=0; row<m_nRows; row++)
{
m_ptr[row] = new T[m_nCols];
assert(m_ptr[row]!= NULL);
}
for(int r=0; r<m_nRows; r++)
for(int c=0; c<m_nCols; c++)
m_ptr[r][c] = m.m_ptr[r][c];
}
template <class T>
Image<T>::~Image()
{
FreeMem();
}
template <class T>
void Image<T>::FreeMem()
{
int i;
if( m_ptr != NULL)
{
for(i=0;i<m_nRows;i++)
{
if( m_ptr[i] != NULL )
{
delete []m_ptr[i]; // delete the datablock
m_ptr[i] = NULL;
}
}
delete []m_ptr; // Delete the row pointers
m_ptr = NULL;
}
}
template <class T>
void Image<T>::Initialize(T x)
{
for(int r=0; r<m_nRows; r++)
for(int c=0; c<m_nCols; c++)
m_ptr[r][c] = x;
}
template <class T>
int Image<T>::ResetSize(int rows, int cols)
{
assert(rows>0 && cols>0); // make sure the dimensions are legal
FreeMem(); // Deallocate if neccessary
m_nRows = rows;
m_nCols = cols;
m_ptr = new T*[m_nRows];
assert(m_ptr != NULL);
for(int row=0; row<m_nRows; row++)
{
m_ptr[row] = new T[m_nCols];
assert(m_ptr[row]!= NULL);
}
return 1;
}
template <class T>
void Image<T>:

perator=(const Image& rhs)
{
if(this != &rhs)
{
ResetSize(rhs.m_nRows, rhs.m_nCols);
for(int r=0; r<m_nRows; r++)
for(int c=0; c<m_nCols; c++)
m_ptr[r][c] = rhs.m_ptr[r][c];
}
return *this;
}
template <class T>
T& Image<T>::CellAt( int row, int col)
{
assert((0<= row) &&(row<m_nRows));
assert((0<= col) &&(col<m_nCols));
return m_ptr[row][col];
}
template <class T>
int Image<T>::Read( const char* fn)
{
ifstream ins;
ins.open(fn);
if(ins.bad() )
{
cerr<<"Input file:"<<"["<<fn<<"]"<<" open failed!"<<endl;
return 0;
}
cout << "file opened to read" << endl;
long size = m_nRows * m_nCols * sizeof(T);
int i,j;
for(i=0; i < m_nRows; i++)
{
for(j=0; j < m_nCols; j++)
ins.read((char*)&m_ptr[i][j], sizeof(T));
}
ins.close();
return size;
}
template <class T>
int Image<T>::Write( const char* fn)
{
ofstream outs;
outs.open(fn);
if(outs.bad())
{
cerr<<"Output file:"<<"["<<fn<<"]"<<" open failed!"<<endl;
return 0;
}
long size = m_nRows * m_nCols * sizeof(T);
int i,j;
for(i=0; i < m_nRows; i++)
{
for(j=0; j < m_nCols; j++)
outs.write((char*)&m_ptr[i][j], sizeof(T));
}
outs.close();
return size;
}