wxArrayInt bracket [] operator for value assignment

Carsten A. Arnholm arnholm at offline.no
Sat Jul 21 04:15:28 PDT 2007


Gregory.A.Book at gmail.com wrote:
> I'm trying to set the value of an element in a wxArrayInt, however I
> get an error. I'm using the following code to attempt the assignment:
>
> wxArrayInt chips[28];
> chips[i] = num;
>
>
> The compile error:
>
> d:\dominoserver2\mainframe.cpp(354) : error C2679: binary '=' : no
> operator found which takes a right-hand operand of type 'int' (or
> there is no acceptable conversion)
>        d:\wxwidgets-2.8.3\include\wx\dynarray.h(994): could be
> 'wxArrayInt &wxArrayInt::operator =(const wxArrayInt &)' while trying
> to match the argument list '(wxArrayInt, int)'
>
> I'm able to use the bracket operator to get values from the elements.
> What might be wrong with the assignment?
> -Greg

Hi,

I could be wrong here (I have little experiene with wxArrayInt etc.), so 
read with scepticism...

>From the first compiler error, it looks like the operator[] could be 
returning a value rather than a reference to the array element in question, 
i.e. not an lvalue. The second message seems to confirm it doesn't find an 
operator=(wxArrayInt, int), which I believe really should require a 
reference as the first argument for a successful assignment. This 
interpretation seems to explain why you can get values, but not assign: it 
returns a value, but not an lvalue.

But again, I could be wrong. I tried to take a look at the header files and 
online documentation for wxArray, but it is quite complex and hard to follow 
in my opinion.

Related note: I enjoy wxWidgets and think it is a marvellous library that 
enables me to write GUI software that runs on multiple systems. It also has 
excellent support for non-GUI things. The only area where I have been a bit 
surprised is: Container classes. I can understand that historically, the C++ 
standard library (of which STL is a part) was not very well supported on all 
relevant platforms, but the C++ ISO standard (including the C++ standard 
library) is now 10 years old. Which relevant compilers have inferior or no 
STL support today?

I have used STL since ~1995 on Windows using various compilers and I am now 
working on a wxWidgets application that uses STL on Windows (VS2005 Express) 
and Linux (GCC), and I observe no issues so far. To make a "dynamic array" 
of ints with initial length 28 and assign values using [], the following 
should be an example of standard, portable C++ for what you are trying to 
do:

#include <vector> // standard C++ header
typedef std::vector<int> myArrayInt;
...
myArrayInt chips(28);
for(size_t i=0; i<chips.size(); i++)  chips[i] = i;

Just my thoughts.

Best regards
Carsten A. Arnholm
http://arnholm.org/
N59.776 E10.457 








More information about the wx-users mailing list