Trouble in accessing items in RArray???

Login to reply to this topic.
Wed, 2007-11-07 03:48
Joined: 2007-04-22
Forum posts: 3

i have a piece of code below:

CMyClass.h
class CMyClass: public CBase
{
enum TListenerType
{
EListener1,
EListener2
};

typedef RArray< TAny* > TListenerList;

typdedef RArray< TListenerList > TListenerHandler;

TListenerHandler iHandlers;
public:
TListenerList& ListenerHandler(TListenerType aType);
};

CMyClass.cpp
......................
CMyClass::TListenerList& CMyClass::ListenerHandler(TListenerType aType)
{
return iHandlers[aType];/// USER 130
}
......................

when i set aType = EListener1 or aType = EListener2, it raises USER 130
means that the index of iHandlers is out of bound.

Can someone explain why?

ht_it
Regard,


Wed, 2007-11-07 10:42
Joined: 2003-12-05
Forum posts: 587
Re: Trouble in accessing items in RArray???

Well this happens because you have less elements in your array than the parameter value is. E.g. if aType equals 1 (EListener2) and your array has 0 or one elements, the array is out of bounds (you try to access an element which cannot be in the array).

Thu, 2007-11-08 02:17
Joined: 2007-04-22
Forum posts: 3
Re: Trouble in accessing items in RArray???

Andreas,thank you for replying.

now, i understand,but i don't know how to initialize iHandlers(e.g 3 elements),so that i can access the array.
Do you have any suggestion?

ht_it
Regard,

Thu, 2007-11-08 10:50
Joined: 2003-12-05
Forum posts: 587
Re: Trouble in accessing items in RArray???

Well, it is a dynamic array. You need to add elements to it. Now it is apparently just empty. See the array's Append/AppendL methods. And please read introductory topics on these issues. If you do not understand what is a dynamic array, I guess you have a hard path ahead of you. Stop coding for a while and read more.

  • Login to reply to this topic.