Hi,
I understand that there must be a copy constructor if I
want to use an array for my class to become a collection.
This is the definition of my class:
class CEntity
{
public:
int num_inPort;
int num_outPort;
int entityCount;
CArray<CString, CString> inPortNameArray;
CEntity();
CEntity(const CEntity& initE);
virtual ~CEntity();
};
My copy constructor for my CEntity class is as follows:
CEntity::CEntity(const CEntity& initE)
{
num_inPort = initE.num_inPort;
num_outPort = initE.num_outPort;
entityCount = initE.entityCount;
int i=0;
int n=0;
while(i<inPortNameArray.GetSize())
{
inPortNameArray.ElementAt(i)
=initE.inPortNameArray.ElementAt(n);
i++;
n++;
}
}
However this produces an error error C2662: 'ElementAt' :
cannot convert 'this' pointer from 'const class
CArray<class CString,class CString>' to 'class
CArray<class CStr
ing,class CString> &'.
Can anyone enlgihten me on this problem? and what are the
solutions?
[quoted text, click to view] >-----Original Message-----
>Hi Ellie,
>
>For example:
>
>void CEntity::AddPortName(const CString strPortName){
> inPortNameArray.Add(strPortName);
>}
>
>I don't know what you are trying to do with this class,
but the goal should
>be to shift as much internal logic as possible into the
class itself. This
>method might look trivial but it means you can change
the internal structure
>of CEntity without having to change code throughout the
rest of your
>program.
>
>Cheers
>
>Doug Forster
>
>
>"ellie" <anonymous@discussions.microsoft.com> wrote in
message
>news:0b3101c3d921$da926040$a301280a@phx.gbl...
>> What do you mean by a small accessor? Could you give an
>> example with reference to my instance?
>>
>>
>> >-----Original Message-----
>> >That will work, but is really bad style. You should
>> write a small accessor
>> >method to add the value so you do not create
>> dependencies on the internal
>> >structure of CEntity scattered all over your program.
>> inPortNameArray should
>> >be private to enforce this. In fact I would never make
>> any member variables
>> >public, though some may disagree with that.
>> >
>> >Cheers
>> >
>> >Doug Forster
>> >
>> >"MdAZ" <anonymous@discussions.microsoft.com> wrote in
>> message
>> >news:889CE7E2-59BB-4916-837E-
>> A1977460BCC7@microsoft.com...
>> >> Try:
>> >>
>> >> R1.inPortNameArray.Add("V1");
>> >> R1.inPortNameArray.Add("V2");
>> >
>> >
>> >.
>> >
>
>
>.
After that I tried your method the following error pops
up:
error C2582: 'CEntity' : 'operator =' function is
unavailable
c:\program files\microsoft visual studio\vc98
\mfc\include\afxtempl.h(1566) : while compiling class-
template member function 'void __thiscall CArray<class
CEntity,class CEntity &>::SetAtGrow(int,class CEntity &)'
what does this mean?
[quoted text, click to view] >-----Original Message-----
>Hi Ellie,
>
>This is because you declared the second template
parameter as a reference.
>Try CArray<CString, CString>
>
>Cheers
>
>Doug Forster
>
>"ellie" <anonymous@discussions.microsoft.com> wrote in
message
>news:0a5b01c3d922$532c6640$a601280a@phx.gbl...
>> By the way, I tried MdAZ's method and this has resulted
>> in an error as follows:
>> "error C2664: 'Add' : cannot convert parameter 1
>> from 'char [3]' to 'class CString &' A reference that
is
>> not to 'const' cannot be bound to a non-lvalue"
>>
>> Does anyone knows what's the problem with this method?
>>
>> >-----Original Message-----
>> >That will work, but is really bad style. You should
>> write a small accessor
>> >method to add the value so you do not create
>> dependencies on the internal
>> >structure of CEntity scattered all over your program.
>> inPortNameArray should
>> >be private to enforce this. In fact I would never make
>> any member variables
>> >public, though some may disagree with that.
>> >
>> >Cheers
>> >
>> >Doug Forster
>> >
>> >"MdAZ" <anonymous@discussions.microsoft.com> wrote in
>> message
>> >news:889CE7E2-59BB-4916-837E-
>> A1977460BCC7@microsoft.com...
>> >> Try:
>> >>
>> >> R1.inPortNameArray.Add("V1");
>> >> R1.inPortNameArray.Add("V2");
>> >
>> >
>> >.
>> >
>
>
>.
Hi Ellie,
It means you are trying to make a CArray of CEntity's and you have not
defined an assignment operator for CEntity. It is often easier to work with
arrays of pointers to objects rather than the objects themselves.
Cheers
Doug Forster
[quoted text, click to view] "ellie" <anonymous@discussions.microsoft.com> wrote in message
news:090001c3db68$b521f890$a601280a@phx.gbl...
> After that I tried your method the following error pops
> up:
>
> error C2582: 'CEntity' : 'operator =' function is
> unavailable
> c:\program files\microsoft visual studio\vc98
> \mfc\include\afxtempl.h(1566) : while compiling class-
> template member function 'void __thiscall CArray<class
> CEntity,class CEntity &>::SetAtGrow(int,class CEntity &)'
>
> what does this mean?
>
>
> >-----Original Message-----
> >Hi Ellie,
> >
> >This is because you declared the second template
> parameter as a reference.
> >Try CArray<CString, CString>
> >
> >Cheers
> >
> >Doug Forster
> >
> >"ellie" <anonymous@discussions.microsoft.com> wrote in
> message
> >news:0a5b01c3d922$532c6640$a601280a@phx.gbl...
> >> By the way, I tried MdAZ's method and this has resulted
> >> in an error as follows:
> >> "error C2664: 'Add' : cannot convert parameter 1
> >> from 'char [3]' to 'class CString &' A reference that
> is
> >> not to 'const' cannot be bound to a non-lvalue"
> >>
> >> Does anyone knows what's the problem with this method?
> >>
> >> >-----Original Message-----
> >> >That will work, but is really bad style. You should
> >> write a small accessor
> >> >method to add the value so you do not create
> >> dependencies on the internal
> >> >structure of CEntity scattered all over your program.
> >> inPortNameArray should
> >> >be private to enforce this. In fact I would never make
> >> any member variables
> >> >public, though some may disagree with that.
> >> >
> >> >Cheers
> >> >
> >> >Doug Forster
> >> >
> >> >"MdAZ" <anonymous@discussions.microsoft.com> wrote in
> >> message
> >> >news:889CE7E2-59BB-4916-837E-
> >> A1977460BCC7@microsoft.com...
> >> >> Try:
> >> >>
> >> >> R1.inPortNameArray.Add("V1");
> >> >> R1.inPortNameArray.Add("V2");
> >> >
> >> >
> >> >.
> >> >
> >
> >
> >.
> >
If my class consisted only of integer variables then this problem will not arise. The problem only arises when I try to add a CStringArray
This is my class
class CEntity
{
public:
int num_inPort;
int num_outPort;
int entityCount;
CStringArray inPortNameArray;
CEntity();
//CEntity(int nip, int nop, int ec, char* ipName[100]);
CEntity(const CEntity& initE);
virtual ~CEntity();
};
and i also had a copy constructor
CEntity::CEntity(const CEntity& initE)
{
num_inPort = initE.num_inPort;
num_outPort = initE.num_outPort;
entityCount = initE.entityCount;
for(int i=0; i<inPortNameArray.GetSize(); i++)
{
inPortNameArray.GetAt(i)=initE.inPortNameArray.GetAt(i);
}
}
I tried to add my information in the following manner:
void CMydrawing1Doc::RetrieveInfo()
{
CEntity R1;
R1.num_inPort = 1;
R1.num_outPort = 1;
R1.entityCount = 1;
R1.inPortNameArray.Add("V1");
CEntity R2;
R2.num_inPort = 1;
R2.num_outPort = 1;
R2.entityCount = 2;
R2.inPortNameArray.Add("V1");
CEntity R3;
R3.num_inPort=1;
R3.num_outPort=1;
R3.entityCount=3;
R3.inPortNameArray.Add("V1");
EntityArray.Add(R1);
EntityArray.Add(R2);
EntityArray.Add(R3);
}
Is it still the problem of assignment operator? and if so how do i declare that? or is there something wrong with the class declaration or copy constructor?
Don't see what you're looking for? Try a search.