C2674 Error W/ Derived Class Operator=
William G Dempsey -- DEMPSEY@dempsey.com Monday, August 26, 1996 Environment: VC 1.52, MFC 2.52, WfW 3.11 I am getting an error message C2674: '=' : no acceptable conversions from 'int' to 'class ::CWDString' from code that attempts to initialize a variable. The variable is "RecVer" (shown below) declared as CWDString wich is a class derived from CString. CWDString has a empty constructor. I was expecting that the operator = implementation of the base class would be available in the derived class. Perhaps I am misinterpreting the error message. Any ideas? Thanks in advance, William class CWDString : public CString {}; void CWDConfig::InitCfg() { CWDString RecVer; RecVer = "00"; // This is line 42 } clas.cpp(42) : error C2674: '=' : no acceptable conversions from 'int' to 'class ::CWDString' ------------------------------------------------------------------------------ William G Dempsey William G Dempsey & Associates Tel: (408) 248-0544 840 Jefferson Street FAX: (408) 248-0544 dempsey@dempsey.com Santa Clara, CA 95050 ------------------------------------------------------------------------------
John Young -- jyoung@gol.com Thursday, August 29, 1996 [Mini-digest: 10 responses] Hi William, ---------- > From: William G Dempsey> To: mfc-l@netcom.com > Subject: C2674 Error W/ Derived Class Operator= > Date: 27 August 1996 11:34am > > Environment: VC 1.52, MFC 2.52, WfW 3.11 > > I am getting an error message C2674: '=' : no acceptable conversions > from 'int' to 'class ::CWDString' from code that attempts to initialize > a variable. > > The variable is "RecVer" (shown below) declared as CWDString wich is a > class derived from CString. CWDString has a empty constructor. I was > expecting that the operator = implementation of the base class would be > available in the derived class. > > Perhaps I am misinterpreting the error message. > > Any ideas? Yup. Derived classes do not inherit constructors and overloaded operators. You have to re-do them yourself in the derived class. Its a pain, I know, but cannot be avoided. -John -----From: "Tomasz Pawlowski" Just add one more constructor: CWDString(const char *s):CString(s) { } This allows to construct CWDConfig object from 'const char*' (i.e. from "00"), and then in your statement RecVer = "00"; // This is line 42 memberwise copy will be applied (assuming that you didn't implement assignment operator). Tomasz tomasz@ix.netcom.com -----From: "S. Balachandar" Hi!, The problem in your case is, the '=' operator is not inherited!!!. Please note : The only operator that is not Inherited is the assignment operator!!. I think this helps you. Regards Bala ----------------------------------------------------------------------------------------------------------------- S. Balachandar e-mail : chandar@adca01.enet.dec.com Software Engineer Phone : 3370445 Ext : 479 Digital Equipment (I) Ltd., Fax : 3371498 Digital House, 45/14 Tumkur Road, Yeshwanthpur II Stage, Bangalore 560 022. -----From: IAND@wcom.co.uk You're not misinterpreting the message. operator=3D is not inherited by a derived class. You'll need to explicitly declare operator=3D and cut and paste some code f= rom=20 the MFC sources. -----From: Dean Henkel You still need to overload the = operator for the type of operation you are performing. The other alternative is to initialize all data members of RecVer in the constructor of CWDString. My preferred way assuming you never need to re-initialize. Dean -----From: Zhilin Li According to Bjarne Stroustrup's book "The C++ Programming Language", operator functions are inherited except operator=(). Have a nice day! Zhilin -- ===================================================================== Zhilin Li Infolytica Corporation Software Developper 300 Leo Pariseau #2222 Tel: (514) 849-8752 ext. 252 P.O.Box 1144, Place du Parc Fax: (514) 849-4239 Montreal, QC, Canada, H2W 2P4 e-Mail: zhilin@infolytica.qc.ca ===================================================================== -----From: David Little You need to write your own "operator =". Operators aren't inherited. Here is an example: class CMyString : public CString { public: operator=(LPCSTR str) { char* s = GetBuffer(strlen(str)); //allocate memory from CString strcpy(s,str); //copy string ReleaseBuffer(); //release memory FreeExtra(); } }; You can also write operators for other data types. I have a CString-derived class that accepts ints, longs, and floats as assignment. I just do a Format() when I get those.... Hope this helps! David -----From: "Lee, Benny" operator= and the copy ctor is not inherited. Benny -----From: Peter Turpin >From VC 1.52 C++ Language help The assignment operator has some additional restrictions. It can be overloaded only as a nonstatic member function, not as a friend function. It is the only operator that cannot be inherited; a derived class cannot use a base class's assignment operator.---------- -----From: "(Bobby)Babak Lashkari" overloaded operator = is not inherited and neither is the copy constructor. you should implement a operator = for your new class and copy the code from CString::operator =. Babak Lashkari (604)221-8996
| Вернуться в корень Архива |