hello,
Am getting the below error while compiling my project.

error C2440: 'initializing' : cannot convert from 'struct __POSITION *' to 'class CPtrList &'
A reference that is not to 'const' cannot be bound to a non-lvalue

This is the snippet of code where the error happens. Tell me what change i have to make in this line to make it error free...

{
......
......
CPtrList& templateList = AfxGetApp()->GetFirstDocTemplatePosition();
.....
.....
.....
}
Am doing migration from 16 bit code to 32 bit... help me to solve the issue...

>>Am doing migration from 16 bit code to 32 bit... help me to solve the issue...
There were some very major changes to MFC between the two versions. Which 32-bit version of the compiler are you using? I hopt it will be vc++ 2010.


Look up the function GetFirstDocTemplatePosition() and you will easily see what the problem is. This should have taken you no more than 60 seconds to find the problem with your code. Once you get the POSITION returned by that function you have to call GetNextDocTemplate() to get a pointer to the CDocTemplate class in which the CPtrList object exists that you are looking for (there could be more than one CDocTemplate objects in the program). Once you get that, you can typecast it to your specific document class (CDocument is derived from CDocTemplate), such as CMyDocument, and that will let you gain access to the CPtrList object in the document.

I use Visual C++ 6.0.
I am new to this MFC and really stuck up to proceed further
I will post the part of the code where the error happen can you guide me....

{
CPtrList m_templateList;
    [B]CPtrList templateList = AfxGetApp()->m_templateList;[/B]
    if (templateList.IsEmpty())
    {
        TRACE0("Error : no document templates registered with CWinApp\n");
        AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
        return NULL;
    }


    POSITION pos = templateList.GetHeadPosition();
    CDocTemplate* pTemplate = (CDocTemplate*)templateList.GetNext(pos);
    ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CMultiDocTemplate)));
    return (CMultiDocTemplate*)pTemplate;
}

this throwed the error m_templateList is not a member of 'CWinApp'

this was the code present in 16 bit code. When I added a line to convert it to 32 bit I added

{  
	CPtrList m_templateList;
	[B]CPtrList& templateList =AfxGetApp()->GetFirstDocTemplatePosition();[/B]
	if (templateList.IsEmpty())
	{
		TRACE0("Error : no document templates registered with CWinApp\n");
		AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
		return NULL;
	}


	POSITION pos = templateList.GetHeadPosition();
	CDocTemplate* pTemplate = (CDocTemplate*)templateList.GetNext(pos);
	ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CMultiDocTemplate)));
	return (CMultiDocTemplate*)pTemplate;
}

this throwed the error
error C2440: 'initializing' : cannot convert from 'struct __POSITION *' to 'class CPtrList &'
A reference that is not to 'const' cannot be bound to a non-lvalue

>>CPtrList& templateList =AfxGetApp()->GetFirstDocTemplatePosition();


That makes no sense at all. You need to do some serious reading if you intend to use MFC. Look up the functions you are trying to use and read what they return. In this case the function return POSITION object, not CPtrList object. You can't just toss random code at a program and expect it to work.

Yes, MFC is difficult to learn -- I'v read the average learning curve is about one year to learn it well.

this was the code snippet that was present in the 16 bit code.

to your knowledge can you give me any changes to the above code snippet to make it work?

thanks

Just delete all the code previous to the line starting with POSITION. Its all useless code anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.