15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


template usage

Zheng Huang -- zhehua@symix.com
Monday, March 10, 1997

Environment: VC++ 4.2-flat, NT 3.51

Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
followed the example in that book but I got following error. I double
checked the syntax. It looks exactly same as one in that book It is
"CTypedPtrList m_strokeList;"

H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: 'struct __POSITION
*CObList::AddHead(class CObject *)' : cannot convert parameter 1 from
'class CStroke *' to 'class CObject *' (new behavior; please see help)

Thanks in advance!

Zheng Huang
zhehua@symix.com




Mike Blaszczak -- mikeblas@nwlink.com
Sunday, March 16, 1997

[Mini-digest: 3 responses]

>---------------------
>You see that in book they use forward definition of CStroke, it
>doesn't work. Is this because VC++ doesn't support forward definition
>of classes with templates?

CStroke isn't a template class.  VC++ does support forward definitions
of templated classes.

>Or is this a bug in CTypedPtrList<>?

No, it isn't.

The problem is that CTypedPtrList was changed before the release of 
MFC 4.2-flat, to do stricter type checking of its parameters.  That
type checking demands that a class be fully declared before it is used
with CTypedPtrList--CTypedPtrList needs to know that the objects it is
collecting are derived from CObject.

The way the tutorial has you write the sample uses a foward declaration.
That incomplete declaration causes the error: the compiler doesn't know
that CStroke derives from CObject and makes an error when CTypedPtrList
demands that derivation.

The documentation in Visual C++ 5.0 was corrected to reflect this
new requirement.

>May be we should ask somebody from Microsoft (i.e. Mike Blaszczak)?

It only takes a little knowledge of the language and a quick look at
the code in the sample on the CD to answer this question correctly.
It's also been covered on the list before, back when the changed code
started shipping in VC++ 4.2.

Is it just my perception, or does it seem like I'm having to spend
more and more time on this list refuting bad advice and incorrect
answers?


.B ekiM
http://www.nwlink.com/~mikeblas/
These words are my own. I do not speak on behalf of Microsoft.
       One is too many and a million is not enough.
-----From: Zheng Huang 

Thanks for all your helps.

As several people suggested, I moved CStroke definition up. It compiled
fine. I also looked at KB Article ID:Q154105. It seems that MFC
templated type safe collection class doesn't work with forward
declaration of class.

Thank you all again!

Zheng Huang

>----------
>From: 	derek.groft@utiligent.com[SMTP:derek.groft@utiligent.com]
>Sent: 	Thursday, March 13, 1997 4:14 PM
>To: 	mfc-l@netcom.com
>Subject: 	Re: template usage
>
>[Mini-digest: 8 responses]
>
>
>I vaguely remember this issue from helping some beginners out earlier this
>year.  I think the code in the book does not compile as written.  I believe
>the order of the classes is wrong (or it is missing a forward declaration).
> I don't have that book in front of me, but if I remember correctly, there
>are two classes declared in the CStroke header file.  If you move the
>second class above the first class, I believe it will compile.  
>
>I hope this helps, if not, let me know and I'll search a little harder for
>the book.  I know I have solved that problem for several people around
>here.
>
>----------
>> From: Zheng Huang 
>> To: 'MFC-L' 
>> Subject: template usage
>> Date: Monday, March 10, 1997 4:28 PM
>> 
>> Environment: VC++ 4.2-flat, NT 3.51
>> 
>> Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
>> followed the example in that book but I got following error. I double
>> checked the syntax. It looks exactly same as one in that book It is
>> "CTypedPtrList m_strokeList;"
>> 
>> H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: 'struct __POSITION
>> *CObList::AddHead(class CObject *)' : cannot convert parameter 1 from
>> 'class CStroke *' to 'class CObject *' (new behavior; please see help)
>> 
>> Thanks in advance!
>> 
>> Zheng Huang
>> zhehua@symix.com
>-----From: Bob Farrell 
>
>At 05:28 PM 3/10/97 -0500, you wrote:
>>Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
>>followed the example in that book but I got following error. I double
>>checked the syntax. It looks exactly same as one in that book It is
>>"CTypedPtrList m_strokeList;"
>
>Yes, I encountered this too.  The fix is to not merely give a forward
>declaration of class CStroke just before the CScribbleDoc definition (as 
>is recommended on p. 36 of the Scribble tutorial), but rather place the 
>entire definition of the CStroke class *before* the CScribbleDoc 
>definition.  Note that this is also contrary to the instruction on p.
>42 that tells you to place the CStroke definition at the end of the
>header file.
>
>The problem has to do with the template class CTypedPtrList member
>variable m_strokeList in CScribbleDoc.  If you look at the Scribble
>source on the CD ROM, you'll see that they didn't follow their own
>instructions.
>
>Bob Farrell
>Marquette Medical Systems
>
>-----From: Joao Vargem 
>
>Zheng Huang wrote:
>> 
>> Environment: VC++ 4.2-flat, NT 3.51
>> 
>> Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
>> followed the example in that book but I got following error. I double
>> checked the syntax. It looks exactly same as one in that book It is
>> "CTypedPtrList m_strokeList;"
>> 
>> H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: 'struct __POSITION
>> *CObList::AddHead(class CObject *)' : cannot convert parameter 1 from
>> 'class CStroke *' to 'class CObject *' (new behavior; please see help)
>> 
>	Hi all, Zheng.
>	My guess is this: CObList receives, CObject type pointes. For the class
>CTypedPtrList
>convert your CStroke class to CObject, it must derive from CObject. If
>it already does, forget this mail, if it doesn't... Bye
>
>-- 
>				Joao Vargem	(jpv.loyaltech@taguspark.pt)
>				LoyalTech Portugal SA
>				Oeiras, Portugal
>-----From: "Rod Denisyuk" 
>
>10-Mar-97 17:28 you wrote:
>> Environment: VC++ 4.2-flat, NT 3.51
>>
>> Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
>> followed the example in that book but I got following error. I double
>> checked the syntax. It looks exactly same as one in that book It is
>> "CTypedPtrList m_strokeList;"
>>
>> H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: 'struct __POSITION
>> *CObList::AddHead(class CObject *)' : cannot convert parameter 1 from
>> 'class CStroke *' to 'class CObject *' (new behavior; please see help)
>>
>> Thanks in advance!
>>
>> Zheng Huang
>> zhehua@symix.com
>>
>
>Hi,
>
>I have the same problem.
>But if you compare you text with source code of Step 1(which compiles
>without errors) you will find the difference:
>
>in file ScribbleDoc.h in book
>
>class CStroke;
>
>class CScribbleDoc
>{
>....
>  "CTypedPtrList m_strokeList;"
>....
>}
>
>class CStroke
>{
>....
>}
>
>--------------------
>
>in file ScribDoc.h in source (Step 1)
>
>class CStroke
>{
>....
>}
>
>class CScribbleDoc
>{
>....
>  "CTypedPtrList m_strokeList;"
>....
>}
>
>---------------------
>
>You see that in book they use forward definition of CStroke, it
>doesn't work. Is this because VC++ doesn't support forward definition
>of classes with templates? Or is this a bug in CTypedPtrList<>?
>
>May be we should ask somebody from Microsoft (i.e. Mike Blaszczak)?
>
>Bye!
>
>Rod
>
>P.S.
>What does it mean 'new behavior' in error messages? What is the
>difference from 'old behavior'?
>
>
>-----From: Alberto Massari 
>
>At 05:28 PM 10/3/97 -0500, Zheng Huang wrote:
>>Environment: VC++ 4.2-flat, NT 3.51
>>
>>Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
>>followed the example in that book but I got following error. I double
>>checked the syntax. It looks exactly same as one in that book It is
>>"CTypedPtrList m_strokeList;"
>>
>>H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: 'struct __POSITION
>>*CObList::AddHead(class CObject *)' : cannot convert parameter 1 from
>>'class CStroke *' to 'class CObject *' (new behavior; please see help)
>
>Is CStroke class derived from CObject? It seems it doesn't....
>If it isn't CObject-derived, use CTypedPtrList
>
>Alberto
>
>			----------------------------
>			    |\      _,,,--,,_
>			    /,`.-'`'   ._  \-;;,_
>			   |,4-  ) )_   .;.(  `'-'
>			  '---''(_/._)-'(_\_)    
>------------------------------------------------------------------
>alby@musart.dist.unige.it is: 	Alberto Massari
>
>Universita' di Genova
>Facolta' di Ingegneria Elettronica
>Laboratorio di Informatica Musicale
>Viale F. Causa 13
>I-16100 Genova, Italia		http://musart.dist.unige.it/alby
>------------------------------------------------------------------
>-----From: Stuart Downing 
>
>>----------
>From:  Zheng Huang[SMTP:zhehua@symix.com]
>>Sent:  Monday, March 10, 1997 5:28 PM
>>To:  'MFC-L'
>>Subject:  template usage
>>
>>Environment: VC++ 4.2-flat, NT 3.51
>
>You should apply the patch to 4.2b.
>
>>
>>"CTypedPtrList m_strokeList;"
>>
>>H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: >'struct __POSITION
>>*CObList::AddHead(class CObject *)' : cannot convert >parameter 1 from
>>'class CStroke *' to 'class CObject *' (new behavior; >please see help)
>
>I suspect your CStroke class is not derived from CObject.
>-----
>Stuart Downing
>sdowning@fame.com
>FAME Information Services, Inc.
>
>
>-----From: kukulies@axpn01.fev.de
>
>Hello Zheng,
>please refer to KB Article ID:Q154105
>
>Benno
>-----From: "Doug Brubacher" 
>
>     In your code is CStroke a CObject derived class?
>     
>     Regards
>     
>     Doug Brubacher
>     Doug_Brubacher@compuware.com
>
>
-----From: Sreekant Sreedharan 

> Can anybody help?  I just started to read "Visual C++ Tutorials" book. I
> followed the example in that book but I got following error. I double
> checked the syntax. It looks exactly same as one in that book It is
> "CTypedPtrList m_strokeList;"
> 
> H:\MSDEV\MFC\include\afxtempl.h(1561) : error C2664: 'struct __POSITION
> *CObList::AddHead(class CObject *)' : cannot convert parameter 1 from
> 'class CStroke *' to 'class CObject *' (new behavior; please see help)

	Your CStroke class, I guess is not derived from CObject. This is the
reason you are getting this problem. This is because CObList requires
that the members in the linked list be derived from CObject.

-- 




Become an MFC-L member | Вернуться в корень Архива |