CArray structures and Error C2664 and C2582 While Using CArray Class


are several error regarding using the CArray inherited structures in c++ MFC programming, such following

case1 : afxtempl.h(443): error C2582:’B’ : ‘operator =’ function is unavailable afxtempl.h(1566):while compiling class-template member function ‘void __thiscall CArray<class B,class B>::SetAtGrow(int,class B)’

case 2: main.cpp(52): error C2664: ‘Add’ : cannot convert parameter 1 from ‘class B’ to ‘class B’ No copy constructor available for class ‘B’

If the class that contains a CArray is nested in another class, then its objects must be copied.
The compiler does not construct an implicit copy constructor and copy assignment operator because the class in question has CArray as a member, which does not have a copy constructor and copy assignment operator, and CArray inherits from CObject, which has a protected copy construtor and a copy assignment operator. The compiler tries to generate the implicit ones, but that generates a call to CObject’s version of them. Because they are protected, the above errors are generated.


The following example shows the correct use of the CArray class:

// No compiler option needed.
#include <afxtempl.h>

struct A
int i;
int j;

class B


// Need to define copy ctor and assignment operator.

B(const B& b){
// Your copy ctor body goes here.

/* const B& */ void operator= (const B& b) {
// Your assignment operator body goes here.

CArray<A, A> arrayA;


class C

void addElement();

CArray<B, B> arrayB;

void C::addElement()
B temp;

void main()


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s