+ 1
Need help...whats wrong with this code?
3 Answers
+ 2
assign mobStats[0]=HP, mobStats[1]=AP, and declere mobStats[2] first
+ 1
There are 3 things you need to change.
on line 6, replace mobStats[] with mobStats[2]. The compiler needs to know how big your array is so it can know much memory to set aside to store its contents.
On line 9, you can't initialize multiple array elements at once like that. Initializing an array with curly braces only works if you're declaring and initializing the array on the same line (The difference between declaring and initializing is that when you declare a function or variable, you're just saying it exists. Initializing is saying what it contains. int x; is declaring. x = 5; is initializing).
For example, this is legal:
int HP, AP;
int mobStats[] = {HP, AP};
This is legal because you can use curly braces to initialize an array on the first line it's declared.
This, however, is illegal:
int HP, AP;
int mobStats[];
mobStats[] = {HP, AP}; //Illegal: can't initialize array with curly braces after declaration
To do what you want to do, you first create an array with 2 slots, then initialize each array slot one at a time like this:
int mobStats[2];
mobStats[0] = HP;
mobStats[1] = AP;
The last thing you need to change is on line 20. You can't call a function with curly braces. You have to use parentheses.
instead of:
Zombie.setStats{2,4};
type:
Zombie.setStats(2,4);
0
thank you very much for your detailed explanation :) it works now and even better is that i understand now aswell why it didn't work before!