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

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


Problems with emmbedded CArrays

Peter -- peter@arrowweb.com
Wednesday, November 27, 1996

Environment: MSVC 4.2b, Windows NT 3.51 SP5

Hi, everyone.  I'm go crazy over this problem... Please help!!

I'm trying to make a variable of type CArray with a CArray as a member in
the variable.  Let me show you what I do:

// definition of the first class (Firstnames)
class Firstnames
{
public:
  Firstnames& operator=(Firstnames& src);
  CString Firstname;
  int age;
};

// 'operator =' definition for Firstnames class
Firstnames& Firstnames::operator=(Firstnames& src)
{
  this->Firstname = src.Firstname;
  this->age = src.age
  return *this;
}

// the second class definition with a CArray of the other 
// class (Firstnames) embedded as a member.
class Surnames
{
public:
  Surnames& operator=(Surnames& src);
  CString theSurname;
  CArray theFirstnames;	
};

// the copy operator of Surnames
Surnames& Surnames::operator=(Surnames& src)
{
  this->theSurname = src.theSurname;
  this->theFirstnames = src.theFirstnames; // crashes here!!
}

// And finally, the variable with the embedded CArray
CArray Families;

the exact error is:  error C2582: 'CArray'
: 'operator =' function is unavailable.  This is occuring, I assume, during
the copy operator of the Surnames class.  But why? the copy operator of
Firstnames is *already* defined!

Below is an example of what I did to cause this:

void MyDocumentClass::FillArray() 
{ 
  Surnames   CurrLast;
  Firstnames CurrFirst; 

  CurrLast.theSurname = "Doe";
  CurrFirst.Firstname = "John";
  CurrFirst.age = 35;
  CurrLast.theFirstnames.Add(theFirstnames);
  
  Families.Add(theSurnames);
}

Any help would be greatly appreciated. 

Peter (peter@arrowweb.com)



Fernando Morgan -- morgan@minisoftpt.com
Saturday, November 30, 1996

> // definition of the first class (Firstnames)
> class Firstnames
> {
> public:
>   Firstnames& operator=(Firstnames& src);
>   CString Firstname;
>   int age;
> };
> 
change the definition to:
>   const Firstnames& operator=(const Firstnames& src);




Michael Potter -- mpotter@ns.ezl.com
Saturday, November 30, 1996

There is no defined operator= for CArray. 

	this->theFirstnames = src.theFristnames; //no definition

You will have to redefine the CArray for your purpose. The best way to
accomplish this is to create a new class and inherit the aspects of CArray

class MyFirstNames : public CArray
{
public:
	MyFirstNames() : CArray() {}
	MyFirstNames& operator=(MyFirstNames& fn); 
};

Now just write the implementation for the operator= method and you are on
your way.

MyFirstNames& MyFirstNames::operator=(MyFirstNames& fn)
{
	//Make sure fn is empty first
	ASSERT(fn.GetSize() == 0);
	Firstname tmp;
	for (int i = 0; i < GetSize();i++)
 	{
		tmp = GetAt(i);
		fn.Add(tmp);
	}
}

Mike

----------
> From: Peter 
> To: mfc-l@netcom.com
> Subject: Problems with emmbedded CArrays
> Date: Wednesday, November 27, 1996 3:42 PM
> 
> Environment: MSVC 4.2b, Windows NT 3.51 SP5
> 
> Hi, everyone.  I'm go crazy over this problem... Please help!!
> 
> I'm trying to make a variable of type CArray with a CArray as a member in
> the variable.  Let me show you what I do:




Alexander Sasha Grinshpun -- alexgr@mercury.co.il
Sunday, December 01, 1996

It seems to me that this question was answered here already.
The reason is that CArray is subclassed from CObject that declares
assignment operator (operator = (const CObject&)) and copy constructor
(CObject(const CObject&)) as private. This prevents from the compiler to
generate default implementation for them. On the other hand, CArray
doesn' implement this functions too. So, you don't have assignment for
CArray. Instead, you can youse  CArray<...>::Copy() method for doing the
same job.

Peter wrote:
> 
> Environment: MSVC 4.2b, Windows NT 3.51 SP5
> 
> Hi, everyone.  I'm go crazy over this problem... Please help!!
> 
> I'm trying to make a variable of type CArray with a CArray as a member in
> the variable.  Let me show you what I do:
> 
> // definition of the first class (Firstnames)
> class Firstnames
> {
> public:
>   Firstnames& operator=(Firstnames& src);
>   CString Firstname;
>   int age;
> };
> 
> // 'operator =' definition for Firstnames class
> Firstnames& Firstnames::operator=(Firstnames& src)
> {
>   this->Firstname = src.Firstname;
>   this->age = src.age
>   return *this;
> }
> 
> // the second class definition with a CArray of the other
> // class (Firstnames) embedded as a member.
> class Surnames
> {
> public:
>   Surnames& operator=(Surnames& src);
>   CString theSurname;
>   CArray theFirstnames;
> };
> 
> // the copy operator of Surnames
> Surnames& Surnames::operator=(Surnames& src)
> {
>   this->theSurname = src.theSurname;
>   this->theFirstnames = src.theFirstnames; // crashes here!!
> }
> 
> // And finally, the variable with the embedded CArray
> CArray Families;
> 
> the exact error is:  error C2582: 'CArray'
> : 'operator =' function is unavailable.  This is occuring, I assume, during
> the copy operator of the Surnames class.  But why? the copy operator of
> Firstnames is *already* defined!
> 
> Below is an example of what I did to cause this:
> 
> void MyDocumentClass::FillArray()
> {
>   Surnames   CurrLast;
>   Firstnames CurrFirst;
> 
>   CurrLast.theSurname = "Doe";
>   CurrFirst.Firstname = "John";
>   CurrFirst.age = 35;
>   CurrLast.theFirstnames.Add(theFirstnames);
> 
>   Families.Add(theSurnames);
> }
> 
> Any help would be greatly appreciated.
> 
> Peter (peter@arrowweb.com)

-- 
----
Thanks,
Sasha.




| Вернуться в корень Архива |