LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2004, 10:10 PM   #1
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Rep: Reputation: 15
c++ templates


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;
}
 
Old 07-29-2004, 12:04 AM   #2
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Rep: Reputation: 30
oh! i thought g++ didnt supported well implementation of template members outside the declaration header file. what version of g++ are u using?
 
Old 07-29-2004, 02:03 AM   #3
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15
Im using borland c++, for windows XP.
 
Old 07-29-2004, 05:34 AM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Following on from Hano's point, read this FAQ and the one after it:

http://www.parashift.com/c++-faq-lit...html#faq-34.12
 
Old 07-29-2004, 08:01 PM   #5
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15
Dakensta,

i read through the whole thing but it really didnt help much, as i still couldnt make an object of class. I basically knew most of what the FAQ was talking about but still couldnt apply it in this case. Can you please give it a shot?

Thans
Shirazi
 
Old 07-30-2004, 05:17 AM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
I guess from your code that the files are in a .h header and a .cpp implementation.

I copied all the source into one header file and wrote an implementation instantiating an image,
Code:
#include <iostream>

#include "bmptmp.hpp"  // copied your source above into this file

int main()
{
  Image<char> I(100, 100);
  Image<char> J();
  Image<char> K(I);
}
and it compiled (albeit with some warnings). I am using g++ 3.3.x, if there any borland specific issues, I am afraid I am unable to comment.

My advice is either to put the declaration and defintion in one file, "image.h", or use the technique mentioned of putting #include "template_definition.cpp" at the bottom of the header file.

HTH
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Impress Templates logosys Linux - Software 1 09-13-2005 10:33 AM
Need help with templates ........ max_rsr Programming 4 04-22-2005 12:53 PM
problem with templates please help..... max_rsr Linux - Newbie 1 04-13-2005 06:51 PM
C++ and templates The_Nerd Programming 4 06-23-2004 01:15 PM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration