Groups | Blog | Home
all groups > visual c > january 2004 >

visual c : Arrays within classes


ellie
1/7/2004 2:54:59 AM
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".

MdAZ
1/7/2004 4:31:16 AM
Try:

R1.inPortNameArray.Add("V1");
Doug Forster
1/8/2004 8:38:01 AM
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

[quoted text, click to view]

ellie
1/12/2004 7:36:32 AM
What do you mean by a small accessor? Could you give an
example with reference to my instance?


[quoted text, click to view]
ellie
1/12/2004 7:39:54 AM
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?

[quoted text, click to view]
Doug Forster
1/13/2004 9:02:28 AM
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


[quoted text, click to view]

Doug Forster
1/13/2004 9:04:08 AM
Hi Ellie,

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

Cheers

Doug Forster

[quoted text, click to view]

ellie
1/14/2004 3:09:36 AM
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]
ellie
1/15/2004 5:08:45 AM
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]
Doug Forster
1/16/2004 9:11:36 AM
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
1/16/2004 7:41:06 PM
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?
AddThis Social Bookmark Button