Click here to Skip to main content
14,880,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have problem when creating the CArray of the object.
On compiling the code i get following error.

>c:\program files\microsoft visual studio 10.0\vc\atlmfc\include\afxwin.h(1986):error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'

C++
CArray< CStaticEx, CStaticEx> m_objStaticExArr;
	m_objStaticExArr.SetSize(nNumOfCamAttch);
	int nXaxis,nYaxis;
	nXaxis = 0; 
	nYaxis = 0 ;
	//CStaticEx objStaticEx;


	for(int i=0; i < nNumOfCamAttch  ; i++){
		CRect rect(nXaxis + X_PADDING, nYaxis + Y_PADDING , nXaxis + WIDTH + X_PADDING  , HEIGHT + Y_PADDING);
		nXaxis = X_PADDING + WIDTH + nXaxis ;
		
		m_objStaticExArr.Add(CStaticEx(rect, this));

	}


What may be the problem in the above code. Please let me know.
Thanks in advance.
Posted
Comments
Richard MacCutchan 28-Oct-13 5:09am
   
Where is the definition of CStaticEx?

1 solution

Hello, if I understand correctly, you are trying to create a dynamic number of label controls.
I currently do not have the source to the CStaticEx class but I can help you through.

First, put an array class in your header file, I recommend CPtrArray or CPtrList.

CPtrArray   m_StaticArray;


Then you will have to add a new and a call to Create() before adding your CStaticEx(s).

int nXaxis,nYaxis;
nXaxis = 0;
nYaxis = 0;

for(int i=0; i < nNumOfCamAttch; i++){
    CRect rect(nXaxis + X_PADDING, nYaxis + Y_PADDING , nXaxis + WIDTH + X_PADDING  , HEIGHT + Y_PADDING);
    nXaxis = X_PADDING + WIDTH + nXaxis ;

    CStaticEx* pStaticCtrl = new CStaticEx;
    pStaticCtrl->Create(_T("Text"), WS_CHILD|WS_VISIBLE, rect, this, nID);

    m_StaticArray.Add(pStaticCtrl);
}



Then, remember to clean it up. In your OnDestroy() handler:

while(m_StaticArray.GetSize() > 0)
{
    CStaticEx* pCtrl = (CStaticEx*)m_StaticArray.GetAt(0);
    m_StaticArray.RemoveAt(0);
    pCtrl->DestroyWindow();
    delete pCtrl;
}


I hope this helps, if I got your goal wrong please let me know and I will help.
   
Comments
Mayur S7789 28-Oct-13 5:52am
   
@JJMatthews: I am trying to create picture control on Dialog dynamically according to some input.
JJMatthews 28-Oct-13 6:04am
   
Then I believe this should help. Like I said, I dont have the source code to the CStaticEx class but you shouldnt have to change much to get it running. Oh yeah, you will need a solution for your control IDs. One way to do it would be to declare a variable in your class header:

static UINT m_nNextID;

then in your cpp file at the top somewhere:

UINT CDialogOrWndClass::m_nNextID = 2050;

make sure this number is greater than the last control value in your resource.h file.

Then call Create() like this:
pStaticCtrl->Create(_T(""), WS_CHILD|WS_VISIBLE, rect, this, m_nNextID++);

good luck
Mayur S7789 28-Oct-13 5:58am
   
Here's the code what i did first. And what i want basically for your better understanding.

CRect rect(10, 10, 300, 300);
m_pStatiEx = new CStaticEx(rect, this);
m_pStatiEx->InitCamDisp(0);
m_pStatiEx->ShowWindow(SW_SHOW);

CRect rect2(320, 10, 620, 300);
m_pStatiEx1 = new CStaticEx(rect2, this);
m_pStatiEx1->InitCamDisp(1);
m_pStatiEx1->ShowWindow(SW_SHOW);

This is the piece of code which i want to implement in for loop according the number of cameras. So want to make the array of object for each camera. Hope now i made you clear with your doubt of what i want to do.
JJMatthews 28-Oct-13 6:05am
   
where is Create() being called?
Mayur S7789 28-Oct-13 6:08am
   
Its called inside the CStaticEx class
CStaticEx::CStaticEx(CRect cRect, CWnd* parent)
{
CStatic::Create(_T("STATIC"), WS_CHILD | WS_VISIBLE, cRect, parent );
m_pFrameImg = NULL;
m_pCamera = NULL;
}
JJMatthews 28-Oct-13 6:13am
   
Ahhh, OK. Then all you have to do is switch out your array class. Check out CPtrArray, CPtrList, CObjArray, or CObjList.
Mayur S7789 28-Oct-13 6:16am
   
What do you mean by "switch out your array class"
JJMatthews 28-Oct-13 6:37am
   
Read up on CPtrArray and CPtrList in MSDN. I avoid CArray (and all templates) if I can. Also, make the array a member variable, you will not need CStaticEx member variables (m_pStatiEx1). Don't forget to clean up your memory in an OnDestroy() handler.
Mayur S7789 28-Oct-13 7:47am
   
Thanks Mate..It worked... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900