View mode: basic / threaded / horizontal-split · Log in · Help
March 31, 2012
Initializing multidimentional Array with a struct
I'm trying to figure out how to initialize a multi-dimentional 
array with a struct. I thought it would be straight forward, but 
i'm running into problems. I'm using nested for loops, and just 
setting the current index to a blank version of my struct but 
that gives me this error: "Error: no [] operator overload for 
type Node". I didn't know I needed to overload that operator, 
usually didn't need to in C++ as far as I remember.

struct Node
{
    bool walkable;		
    vect2 position;		
    int	xIndex, yIndex;
    Node*[4] connections;
}

void InitializePathGraph()
{
    for( int x = 0; x < mapWidth; x++ )
    {
	for( int y = 0; y < mapHeight; y++ )
	{
	    Node node;
	    PathGraph[x][y] = node;// ERROR
	}
    }
}
March 31, 2012
Re: Initializing multidimentional Array with a struct
I also tried this, which gives an out of range error:

	void InitializePathGraph()
	{
		PathGraph.length = mapWidth;
		foreach( elem; PathGraph )
		{
			elem.length = mapHeight;
		}
		
		Node node;

		for( int x = 0; x < mapWidth - 1; x++ )
		{
			for( int y = 0; y < mapHeight - 1; y++ )
			{
				
				PathGraph[x][y] = node;
			}
		}
	}

This is really confusing me.
March 31, 2012
Re: Initializing multidimentional Array with a struct
On 03/31/2012 02:34 PM, Chris Pons wrote:
> I'm trying to figure out how to initialize a multi-dimentional array
> with a struct. I thought it would be straight forward, but i'm running
> into problems. I'm using nested for loops, and just setting the current
> index to a blank version of my struct but that gives me this error:
> "Error: no [] operator overload for type Node". I didn't know I needed
> to overload that operator, usually didn't need to in C++ as far as I
> remember.
>
> struct Node
> {
> bool walkable;
> vect2 position;
> int xIndex, yIndex;
> Node*[4] connections;
> }
>
> void InitializePathGraph()
> {
> for( int x = 0; x < mapWidth; x++ )
> {
> for( int y = 0; y < mapHeight; y++ )
> {
> Node node;
> PathGraph[x][y] = node;// ERROR
> }
> }
> }
>
>

Do you want to initialize with the default value of Node? Then it is as 
easy as the following:

import std.stdio;

struct Node
{}

void main()
{
    Node[2][3] a;       // fixed-length of fixed-length
    Node[][] b = new Node[][](2, 3);  // slice of slice

    writeln(a);
    writeln(b);
}

The output:

[[Node(), Node()], [Node(), Node()], [Node(), Node()]]
[[Node(), Node(), Node()], [Node(), Node(), Node()]]

Please note the different meanings of 2 and 3 for the fixed-length array 
and the new expression: lines and rows are swapped!

Ali
March 31, 2012
Re: Initializing multidimentional Array with a struct
Yes sorry, I was looking to initialize to the default value of 
node. Thank you for the help!

On Saturday, 31 March 2012 at 21:59:50 UTC, Ali Çehreli wrote:
> On 03/31/2012 02:34 PM, Chris Pons wrote:
> > I'm trying to figure out how to initialize a
> multi-dimentional array
> > with a struct. I thought it would be straight forward, but
> i'm running
> > into problems. I'm using nested for loops, and just setting
> the current
> > index to a blank version of my struct but that gives me this
> error:
> > "Error: no [] operator overload for type Node". I didn't know
> I needed
> > to overload that operator, usually didn't need to in C++ as
> far as I
> > remember.
> >
> > struct Node
> > {
> > bool walkable;
> > vect2 position;
> > int xIndex, yIndex;
> > Node*[4] connections;
> > }
> >
> > void InitializePathGraph()
> > {
> > for( int x = 0; x < mapWidth; x++ )
> > {
> > for( int y = 0; y < mapHeight; y++ )
> > {
> > Node node;
> > PathGraph[x][y] = node;// ERROR
> > }
> > }
> > }
> >
> >
>
> Do you want to initialize with the default value of Node? Then 
> it is as easy as the following:
>
> import std.stdio;
>
> struct Node
> {}
>
> void main()
> {
>     Node[2][3] a;       // fixed-length of fixed-length
>     Node[][] b = new Node[][](2, 3);  // slice of slice
>
>     writeln(a);
>     writeln(b);
> }
>
> The output:
>
> [[Node(), Node()], [Node(), Node()], [Node(), Node()]]
> [[Node(), Node(), Node()], [Node(), Node(), Node()]]
>
> Please note the different meanings of 2 and 3 for the 
> fixed-length array and the new expression: lines and rows are 
> swapped!
>
> Ali
Top | Discussion index | About this forum | D home