Arrays within classes

  • Thread starter Thread starter ellie
  • Start date Start date
E

ellie

How do you add elements to an array defined within
another class?

For instance,

class CEntity
{
public:
int num_inPort;
int num_outPort;
int entityCount;
char* inPortName[100];
char* outPortName[100];
CArray<CString, CString&> inPortNameArray;
}

CEntity R1;
R1.num_inPort = 1;
R1.num_outPort = 1;
R1.entityCount = 1;
//how to add the array information here? if let's say
elements in the array are "v1" and "v2".
 
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
 
What do you mean by a small accessor? Could you give an
example with reference to my instance?
 
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?
 
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
 
Hi Ellie,

This is because you declared the second template parameter as a reference.
Try CArray<CString, CString>

Cheers

Doug Forster

ellie said:
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


(e-mail address removed)...


.
 
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?
 
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

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


(e-mail address removed)...
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

ellie said:
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

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

message
(e-mail address removed)...
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?
 
Back
Top