Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CArray ERROR with pointers in VC++ 1

Status
Not open for further replies.

Noelia

Programmer
May 9, 2001
2
0
0
ES
This code compile:

////////////////////////////////////////
MyObject.h
////////////////////////////////////////

class MyObject : public CObject
{
public:
MyObject();
~MyObject();
};

////////////////////////////////////////
MyObject.cpp
////////////////////////////////////////

#include "stdafx.h"
#include "myObject.h"

MyObject::MyObject()
{
}
MyObject::~MyObject()
{
}

////////////////////////////////////////
MyClass.h
////////////////////////////////////////

#include <afxtempl.h>
#include &quot;MyObject.h&quot;
class MyClass : public CObject
{
private:
CArray<MyObject*, MyObject*> arrObject; (1)
public:
MyClass();
~MyClass();
void AddObject( MyObject* Object ); (2)
};

////////////////////////////////////////
MyClass.cpp
////////////////////////////////////////

#include &quot;stdafx.h&quot;
#include &quot;myobject.h&quot;

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

void MyClass::AddObject( MyObject* Object ) (3)
{
arrObject.Add( Object );
}



Case One:
------------------------------------------------------------------

If I replace the lines with number for the next lines

CArray<MyObject, MyObject*> arrObject; (1)
void AddObject( MyObject* Object ); (2)
void MyObject::AddObject( MyObject* Object ) (3)

or this

CArray<MyObject, MyObject&> arrObject; (1)
void AddObject( MyObject& Object ); (2)
void MyObject::AddObject( MyObject& Object ) (3)


the code NOT compile, and in both cases the error is:
&quot;MyObject' : 'operator =' function is unavailable
\\vc98\mfc\include\afxtempl.h(1567) : while compiling class-template member function
'void __thiscall CArray<class MyObject,class MyObject *>::SetAtGrow(int,class MyObject *)'&quot;


Case Two:
------------------------------------------------------------------

If I replace the lines with number for the next lines

CArray<MyObject, MyObject> arrObject; (1)
void AddObject( MyObject Object ); (2)
void MyObject::AddObject( MyObject Object ) (3)

the code NOT compile, and the error is:
&quot;No copy constructor available for class 'MyObject'&quot;

even if the copy constructor is added the error is still reported.

 
Noelia,

This is very interesting but what exactly is your question?
 
The question is &quot;Why not compile in the cases 1 or 2?&quot;
 
I had exactlly your problem and the cause is:

1. in internal manipulation of the arguments you give to CArray it needs the =operator and copy contructor of the class you pass as argument.

2.The default =operator and copy constructor for a CObject derived class ARE NOT AVAILABLE because the are declared PRIVATE in CObject.

Provide =operator and copy constructor for your class derived from CObject and the code will work.

Hope this helps,s-) Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
What do mean exactly with
Provide =operator and copy constructor for your class derived from CObject and the code will work.

Must i add to my Array-class ther Operator = and :: Copy

 
//////declaration MyObject.h//////////////////////////
class MyObject : public CObject
{
public:
MyObject();
MyObject(MyObject& ob) //copy contructor
MyObject& operator=(MyObject& ob); //operator=
~MyObject();
};

//////implementation MyObject.cpp//////////////////////////
MyObject::MyObject(MyObject& ob)
{
m_membervariabile=ob.m_membervariabile
......
m_last_membervariabile=ob.m_last_membervariabile
}
MyObject& MyObject::eek:perator=(MyObject& ob)
{
this->m_membervariabile=ob.m_membervariabile
.......
this->m_last_membervariabile=ob.m_last_membervariabile
return *this;
}

Also read the article &quot;BUG: Error C2664 and C2582 While Using CArray Class&quot; from MSDN(I have April 2001) to understand better the problem.

I remember I had this problem too, and it took me two weeks to realize it at that time.

This should help you,
s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top