nemesis567
Posting Freak
Posts: 874
Threads: 65
Joined: May 2011
Reputation:
10
|
Array in class
I've been trying forever to have an array in a class. It just won't work and will give me an error message. Is there any way to get an array into a class?
Today I dreamt the life I could live forever. You only know that when you feel it for you know not what you like until you've experienced it.
|
|
04-18-2012, 01:00 PM |
|
Cranky Old Man
Posting Freak
Posts: 986
Threads: 20
Joined: Apr 2012
Reputation:
38
|
RE: Array in class
(04-18-2012, 01:00 PM)nemesis567 Wrote: will give me an error message What error message? Always, always specify what error message pops up. Error messages are made to be read and understood, if not by you, then by the person helping you.
(This post was last modified: 04-18-2012, 01:08 PM by Cranky Old Man.)
|
|
04-18-2012, 01:08 PM |
|
nemesis567
Posting Freak
Posts: 874
Threads: 65
Joined: May 2011
Reputation:
10
|
RE: Array in class
It's not specific, and thus won't help that much: Error: Expected ;
The ; was expected where I put "HERE"
int[][] myArray"HERE"(5, int[](5));
Today I dreamt the life I could live forever. You only know that when you feel it for you know not what you like until you've experienced it.
|
|
04-18-2012, 01:47 PM |
|
Cranky Old Man
Posting Freak
Posts: 986
Threads: 20
Joined: Apr 2012
Reputation:
38
|
RE: Array in class
(04-18-2012, 01:47 PM)nemesis567 Wrote: It's not specific, and thus won't help that much: Error: Expected ;
The ; was expected where I put "HERE"
int[][] myArray"HERE"(5, int[](5));
I know very little about AngelScript arrays, but reading the AngelScript documentation, I see that arrays aren't necessarily supported by the HPL2 engine.
Your line looks okay - it should define a 5x5 array of ints with uninitialized values - but judging by the error, it seems that the compiler can only understand empty arrays. ( int[][] myArray; )
|
|
04-18-2012, 02:05 PM |
|
nemesis567
Posting Freak
Posts: 874
Threads: 65
Joined: May 2011
Reputation:
10
|
RE: Array in class
Empty arrays work as long as I don't ever assign anything to them. Pointless.
Today I dreamt the life I could live forever. You only know that when you feel it for you know not what you like until you've experienced it.
|
|
04-18-2012, 02:41 PM |
|
Apjjm
Is easy to say
Posts: 496
Threads: 18
Joined: Apr 2011
Reputation:
52
|
RE: Array in class
The problem comes from the fact that angelscript treats the array initilisation as a function call, which it then doesn't permit in the global scope:
int[][] x(4,int[](5)); -> int[][] x(4, makeArrayInt(5));
One way to fix this is to just to take split the initialisation out so there are none of these function calls:
int[][] x(4,_x1); int[] _x1(5,0);
This works, as can be seen by running the following test:
int[][] x(3,y); int[] y(3,0);
void OnStart()
{
x[0][0] = 1;
x[0][1] = 2;
x[0][2] = 3;
x[1][0] = 10;
x[1][1] = 20;
x[1][2] = 30;
x[2][2] = -3;
x[2][1] = -2;
x[2][0] = -1;
printArray(x,"Test");
printArray(y,"Test2");
}
void printArray(int[] ar, string name)
{
AddDebugMessage(name,false);
for(uint i =0; i<ar.length(); i++) AddDebugMessage("["+i+"]" + ar[i],false);
}
void printArray(int[][] ar, string name)
{
AddDebugMessage(name,false);
for(uint i =0; i<ar.length(); i++)
for(uint j =0; j<ar[i].length(); j++)
AddDebugMessage("["+i+","+j+"]" + ar[i][j],false);
}
The other way is just to state the bounds in a constructor (classes) or the OnStart event:
int[][] a;
void OnStart()
{
a = int[][](4,int[](5,0));
}
Edit: Forum decided to mess up the code tags horribly
(This post was last modified: 04-18-2012, 02:53 PM by Apjjm.)
|
|
04-18-2012, 02:49 PM |
|
nemesis567
Posting Freak
Posts: 874
Threads: 65
Joined: May 2011
Reputation:
10
|
RE: Array in class
Thanks a lot!
If you initialize it with values it also seems to work correctly.
Unfortunetely it still gives the same error. Remember that the array is a member of the class.
Today I dreamt the life I could live forever. You only know that when you feel it for you know not what you like until you've experienced it.
(This post was last modified: 04-18-2012, 02:58 PM by nemesis567.)
|
|
04-18-2012, 02:52 PM |
|
Your Computer
SCAN ME!
Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation:
235
|
RE: Array in class
(04-18-2012, 02:52 PM)nemesis567 Wrote: Unfortunetely it still gives the same error. Remember that the array is a member of the class.
Try declaring the array without a value, with the size of the array provided within the square brackets, then in the constructor of the class assign the default values.
|
|
04-18-2012, 03:06 PM |
|
nemesis567
Posting Freak
Posts: 874
Threads: 65
Joined: May 2011
Reputation:
10
|
RE: Array in class
int myArray[nVal1][nVal2]; Does not work.
int[][] myArray = {{1,2},{1,2}}; Does not work
int[][] myArray(2, int[](2)); Does not work
int[][] x(4,_x1); int[] _x1(5,0); Does not work
int[] simpleArray(2); Does not work
int[][] myArray; Works but when I initialize values in the constructor it gives an error from the main function: main(8,2): Failed to initialize global variable myObject;
Today I dreamt the life I could live forever. You only know that when you feel it for you know not what you like until you've experienced it.
(This post was last modified: 04-18-2012, 03:15 PM by nemesis567.)
|
|
04-18-2012, 03:11 PM |
|
Apjjm
Is easy to say
Posts: 496
Threads: 18
Joined: Apr 2011
Reputation:
52
|
RE: Array in class
The last method should still work for classes:
void OnStart()
{
Test t;
printArray(t.array,"T");
}
class Test
{
int[][] array;
Test()
{
array = int[][](4,int[](5,0));
}
}
void printArray(int[][] ar, string name)
{
AddDebugMessage(name,false);
for(uint i =0; i<ar.length(); i++)
for(uint j =0; j<ar[i].length(); j++)
AddDebugMessage("["+i+","+j+"]" + ar[i][j],false);
}
|
|
04-18-2012, 04:18 PM |
|
|