Return bidimensional array
-
Flaviu2 wrote:
my question is, how can replace vector.resize method...
Have you tried this.
Flaviu2 wrote:
...and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ?
From where?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Ok, resize issue has been solved. Secondly, as long m_arrBitmapData is private memeber, I want to return this memeber through a public method, I mean from this class ... Something like that:
public:
CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > GetBitmapArray(){return m_arrBitmapData;}because this trial give me:
error C2558: class 'CArray<class CArray<unsigned long,unsigned long &>,class CArray<unsigned long,unsigned long &> >' : no copy constructor available
-
Ok, resize issue has been solved. Secondly, as long m_arrBitmapData is private memeber, I want to return this memeber through a public method, I mean from this class ... Something like that:
public:
CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > GetBitmapArray(){return m_arrBitmapData;}because this trial give me:
error C2558: class 'CArray<class CArray<unsigned long,unsigned long &>,class CArray<unsigned long,unsigned long &> >' : no copy constructor available
-
Just return a constant reference as in
const CArray< CArray, CArray > & GetBitmapArray()
The good thing about pessimism is, that you are always either right or pleasently surprised.
I think is working ... :) I have one more question: there is a way to pass a paramanter as CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> >, just like:
// header
void SetPlotData(CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > InputArray);and
// implementation
void CGraphCtrl::SetPlotData(CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > InputArray)
{
if ((m_PixelNumberX>0) && (m_PixelNumberY > 0))
{
PlotData = InputArray;
}
}because code above doesn't work:
error C2582: 'CArray<class CArray<unsigned long,unsigned long &>,class CArray<unsigned long,unsigned long &> >' : 'operator =' function is unavailable
As soon as I solve this error, my program will run ...
-
I have a class (taken from somewhere). Here, I have a member like this:
public:
void SetBitmapDataSize(const int x, const int y);private:
int m_PixelNumberX;
int m_PixelNumberY;
vector > BitmapData;and SetBitmapDataSize is implemented:
void CBitmapOp::SetBitmapDataSize(const int x, const int y)
{
m_PixelNumberX = x;
m_PixelNumberY = y;
if((x>=0) && (y>=0))
{
BitmapData.resize(m_PixelNumberX);
for(int x = 0; x < m_PixelNumberX; x++)
BitmapData[x].resize(m_PixelNumberY);
}
}Now, I want to replace vector > with this CArray: I had tried:
private:
int m_PixelNumberX;
int m_PixelNumberY;
CArray< CArray, CArray > m_arrBitmapData;my question is, how can replace vector.resize method, and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ? Thank you.
I would neither store nor return the bitmap data in that way. What you are creating here is a so-called "ragged" array, in which each row can have a different length. In a bitmap this luxury is not needed, all rows are of equal length by definition. Therefore a one-dimensional array is good enough and in some regards even superior. a) A one-dimensional array can be allocated in one chunk (whereas your ragged array needs N allocations, N being the number of rows) b) A one-dimensional array does not incur the overhead that comes with a ragged array, which is easily 3 x 64bits per row. To access the members of the one-dimensional array in a 2D fashion, simply use the algorithm:
index of pixel (x, y) = x + y\*pixelsPerRow
As a return value for your access function I would suggest you use a simple pointer (const COLORREF*). For the caller to know how to access that 1D array you should provide addition access functions for the number or rows and number of columns.
-
I would neither store nor return the bitmap data in that way. What you are creating here is a so-called "ragged" array, in which each row can have a different length. In a bitmap this luxury is not needed, all rows are of equal length by definition. Therefore a one-dimensional array is good enough and in some regards even superior. a) A one-dimensional array can be allocated in one chunk (whereas your ragged array needs N allocations, N being the number of rows) b) A one-dimensional array does not incur the overhead that comes with a ragged array, which is easily 3 x 64bits per row. To access the members of the one-dimensional array in a 2D fashion, simply use the algorithm:
index of pixel (x, y) = x + y\*pixelsPerRow
As a return value for your access function I would suggest you use a simple pointer (const COLORREF*). For the caller to know how to access that 1D array you should provide addition access functions for the number or rows and number of columns.
-
It is not very difficult to get rid of this 2D array. 1) replace it with a vector m_bitmapData; 2) Replace any reference to the old BitmapData array as follows: BitmapData[x][y] should be replaced by m_bitmapData[x + m_PixelNumberX*y] 3) In SetBitmapDataSize allocate the new array by m_bitmapData.resize (x * y); Change the function interfaces accordingly. Should be really easy.
-
It is not very difficult to get rid of this 2D array. 1) replace it with a vector m_bitmapData; 2) Replace any reference to the old BitmapData array as follows: BitmapData[x][y] should be replaced by m_bitmapData[x + m_PixelNumberX*y] 3) In SetBitmapDataSize allocate the new array by m_bitmapData.resize (x * y); Change the function interfaces accordingly. Should be really easy.
-
It is not very difficult to get rid of this 2D array. 1) replace it with a vector m_bitmapData; 2) Replace any reference to the old BitmapData array as follows: BitmapData[x][y] should be replaced by m_bitmapData[x + m_PixelNumberX*y] 3) In SetBitmapDataSize allocate the new array by m_bitmapData.resize (x * y); Change the function interfaces accordingly. Should be really easy.
-
Great ! Is working, just like the original one ... I didn't replace 2D array by vector, but with CArray ... and is working ! Kindly thank you, NV3 !
-
I have a class (taken from somewhere). Here, I have a member like this:
public:
void SetBitmapDataSize(const int x, const int y);private:
int m_PixelNumberX;
int m_PixelNumberY;
vector > BitmapData;and SetBitmapDataSize is implemented:
void CBitmapOp::SetBitmapDataSize(const int x, const int y)
{
m_PixelNumberX = x;
m_PixelNumberY = y;
if((x>=0) && (y>=0))
{
BitmapData.resize(m_PixelNumberX);
for(int x = 0; x < m_PixelNumberX; x++)
BitmapData[x].resize(m_PixelNumberY);
}
}Now, I want to replace vector > with this CArray: I had tried:
private:
int m_PixelNumberX;
int m_PixelNumberY;
CArray< CArray, CArray > m_arrBitmapData;my question is, how can replace vector.resize method, and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ? Thank you.
Your answer is available on http://techgurulab.com/course/c-study-material/