how to declare variable

D

Daniel

I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;

Daniel
 
D

David Wilkinson

Daniel said:
I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;


Daniel:

What is u? How is it initialized? The question cannot be answered without this
knowledge.
 
G

Giovanni Dicanio

David Wilkinson said:
Daniel said:
I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;


Daniel:

What is u? How is it initialized? The question cannot be answered without
this knowledge.


I think u is an 'unsigned int' or an 'int', and u has values out of range
[0,999].

Giovanni
 
D

Daniel

Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.


David Wilkinson said:
Daniel said:
I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;


Daniel:

What is u? How is it initialized? The question cannot be answered without
this knowledge.
 
G

Giovanni Dicanio

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

So, please show code for your List class, in particular operator[].

Giovanni
 
G

Giovanni Dicanio

Daniel said:
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

OK, I understand that you are using System.Collection.Generic.List<T>
generic container from .NET, from a C++/CLI code I think.

The constructor to which you pass as integer sets list initial capacity, not
its size.

So the list is still empty from operator[] point of view.

I think you should fill the list with code like this:

<code>

List<int> ^ list = gcnew List<int>();

int count = 1000;
for ( int i = 0; i < count; i++ )
{
list->Add( i );
}

</code>

Giovanni
 
D

Daniel

There is no other code. Just . . .

List<unsigned int> ls(1000);

and . . .

ls = 1;

in a loop.

This code doesn't do anything right now. I'm merely using test code to be
sure I understand how to write the code.

Daniel

Giovanni Dicanio said:
I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

So, please show code for your List class, in particular operator[].

Giovanni
 
D

Daniel

Right after . . .

list->Add( i );

.. . . should I write code like . . .

list = <any int value>;

Is that right?

Daniel

Giovanni Dicanio said:
Daniel said:
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

OK, I understand that you are using System.Collection.Generic.List<T>
generic container from .NET, from a C++/CLI code I think.

The constructor to which you pass as integer sets list initial capacity,
not its size.

So the list is still empty from operator[] point of view.

I think you should fill the list with code like this:

<code>

List<int> ^ list = gcnew List<int>();

int count = 1000;
for ( int i = 0; i < count; i++ )
{
list->Add( i );
}

</code>

Giovanni
 
G

Giovanni Dicanio

Daniel said:
Right after . . .

list->Add( i );

. . . should I write code like . . .

list = <any int value>;

Is that right?


Add() allows to insert new data into the List<T> (actually, a vector...).
If you have N items in the List, you can access them using operator[], using
values from 0 (the first item) to N-1 (the last item).

Giovanni
 
B

Ben Voigt [C++ MVP]

Daniel said:
Right after . . .

list->Add( i );

. . . should I write code like . . .

list = <any int value>;

Is that right?


No, but after

list->Add(i);

you should expect that

list[list->Length - 1] == i;

Note that the parameter to Add is the value stored in the list/array, not
the index. In Giovanni's example they are the same though.
Daniel

Giovanni Dicanio said:
Daniel said:
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

OK, I understand that you are using System.Collection.Generic.List<T>
generic container from .NET, from a C++/CLI code I think.

The constructor to which you pass as integer sets list initial
capacity, not its size.

So the list is still empty from operator[] point of view.

I think you should fill the list with code like this:

<code>

List<int> ^ list = gcnew List<int>();

int count = 1000;
for ( int i = 0; i < count; i++ )
{
list->Add( i );
}

</code>

Giovanni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top