Click to See Complete Forum and Search --> : URGENT - template problem - PLEASE HELP


Martin vd Berg
April 7th, 1999, 03:26 AM
Throughout my project I use class members of type CMap e.g.:
CMap<CString, LPCSTR, C_ApplicationPart*, C_ApplicationPart*> m_mapApplicationPart;

Until yesterday the compiler accepted these constructions. But suddenly it reports the following error for every CMap type defined:
C:\Program Files\DevStudio\VC\MFC\include\afxtempl.h(1478) : error C2664: 'SetAt' : cannot convert parameter 1 from 'const char *' to 'class CString &'

This is very odd.... I've reversed changes I've made, tried old project/workspace files but nothing seems to work. I created a testproject and this project compiles CMap<CString, LPCSTR, typeVal, typeArg> with no problems!!!!

I also tried deleting all temporary files and precompiled header files but nothing seems to work.

Please, please help!

Martin van den Berg
Martin.van.den.Berg@hta.nl

Kevin Huang
April 7th, 1999, 04:29 AM
just guess:
the error is raised by SetAt() not by CMap<...> constructor

check the SetAt(), maybe you use
CString str;
SetAt(str, ....);
but not
SetAt((LPCTSTR)str, ....);

just guess...

Martin vd Berg
April 7th, 1999, 04:39 AM
The compile error occures in
afxtempl.h in the CMap::Serialize method which I do not even use!!!

I am confinced it is not in on of my projects SetAt()'s.

Martin van den Berg
High Tech Automation
The Netherlands
Martin.van.den.Berg@hta.nl

ric
April 7th, 1999, 04:41 AM
This error means that you are giving an argument of class CString to SetAt(char *) which actually takes parameter of type (char *).

Try with the CSTring member operator as well
operator LPCTSTR ( ) const;
which converts the CString to a C-string

This casting operator provides an efficient method to access the null-terminated C string contained in a CString object. No characters are copied; only a pointer is returned. Be careful with this operator. If you change a CString object after you have obtained the character pointer, you may cause a reallocation of memory that invalidates the pointer.

Or just try extracting the CString values into a buffer of type (char *) and then give it to the function SetAt(buffer); but I think the first is better and faster.

Martin vd Berg
April 7th, 1999, 04:50 AM
My problem goes beyond casting etc..
The problem is that the compiler reports an error in CMap::Serialize(). In this method the SetAt method is used and this is where the compiler complains about.

I commented this particular piece of code in afxtempl.h out and it now compiles fine! Ofcourse this is a big HACK but it works for the mean time.

I didn't make any mistakes - have a look at the sample in 'Collections: Template-Based Classes.:


Martin van den Berg
High Tech Automation
The Netherlands
Martin.van.den.Berg@hta.nl

Dave Lorde
April 7th, 1999, 11:05 AM
That code ought to be OK.

The CMap::SetAt method expects parameter 1 to be an ARG_KEY type, which you've declared to be LPCSTR. The CMap::Serialize method is passing it a CString, which should convert OK (CString has an LPTCSTR conversion).

The error message says SetAt is expecting a CString&, but is getting passed a const char*, which is obviously not correct - it's the reverse of what you'd expect.

I think that somewhere in your program you have mistakenly defined a CMap with the first two template arguments reversed:

CMap<LPCSTR, CString&, C_ApplicationPart*, C_ApplicationPart*> m_mapApplicationPart;

If you put this into your test program it should give the same error.

Nothing else seems to make sense...

Dave

Martin vd Berg
April 8th, 1999, 04:13 AM
Dave,

Thanks! That was exactly it!!



Martin van den Berg
High Tech Automation
The Netherlands
Martin.van.den.Berg@hta.nl