Hey @jakerichguy,
There are several things wrong with your code …
To push values to an array, you need to call the push method on the array name without the square brackets …
admins.push();
To reference a value within an array we append its index value in square brackets to the array name e.g. admins[0]
will reference the first value in the array (at index 0). So, you are getting the error message Index expression cannot be omitted
because the compiler thinks you want to reference an address within the admins
array, and so it’s expecting an index number after the opening square bracket.
Once you remove the square brackets as I’ve shown you above, you’ll now get the error message …
Member "push" not found or not visible after argument-dependent lookup in address[] storage ref.
It’s not clear what is meant by argument-dependent lookup, but essentially, the compiler is informing you that you can’t push an array value …
[owner, admin1, admin2, admin3]
… to the admins
array (the address[ ] storage reference). This is because admins
is defined as an array containing individual address values e.g.
// address[] admins
[0x74...483, 0x38...819, ...]
… and not as an array containing further arrays of address values e.g.
// address[][] admins
[
[0x74...483, 0x38...819],
[0x32...715, 0x44...588],
...
]
Before you remove the square brackets from the array name admins
(as above), you also get a slightly different version of this error message. Here, the compiler still thinks you are referencing (looking up) a specific address held in the admins
array, and I think it’s basically telling you that you can’t push a value to an address: .push()
is an array method that can only be called on an array.
However, even if you also remove the square brackets from the 4 addresses you want to push to the array, you’ll still get the same error message…
admins.push(owner, admin1, admin2, admin3); // => Error
This is because, as far as I’m aware, you can only use the push method to push a single value at a time to an array e.g.
admins.push(admin1);
The following works, but it is obviously very repetitive and inefficient to code it like this …
admins.push(owner);
admins.push(admin1);
admins.push(admin2);
admins.push(admin3);
As you will already need to have assigned address values to the state variables owner
, admin1
, admin2
and admin3
, my question to you would be: Why do you need to store these addresses in separate state variables AND in an array? Such a duplication of data saved in storage is unnecessarily expensive. I would also think about the following:
-
If you need to store these addresses together in an array, then why not just assign them directly to admins
, and remove the need for the separate address state variables altogether?
-
If there is a fixed number of admin addresses that you need to store together in a data structure, then you should define the array as a fixed-sized array instead of dynamically-sized (e.g. address[4] admins;
) and assign the admin addresses using index numbers instead of the push method. Or you could think about using a struct instead of an array.
Let us know if anything is unclear, or if you have any further questions. And if your code relates to the Multi-Sig project, then please post your questions in the Project - Multisig Wallet discussion topic instead of here.