我想将单个参数作为数组传递给函数调用:
contract MyToken is StandaloneERC20 {
function initialize(
address owner,
address minter,
address[] memory pausers
) public initializer {
address[] memory minters = [minter];
StandaloneERC20.initialize("MyToken", "MTK", uint8(18), minters, pausers);
}
StandaloneERC20.initialize
期望第四个参数minters
的数组:
function initialize(
string memory name, string memory symbol, uint8 decimals, address[] memory minters, address[] memory pausers
) public initializer {
但是,使用我的代码,我得到了稳定度为0.5.7的以下编译器错误:
TypeError: Type address[1] memory is not implicitly convertible to expected type address[] memory.
address[] memory minters = [minter];
^---------------------------------^
我也试图将miner作为数组参数传递:
contract MyToken is StandaloneERC20 {
function initialize(
address owner,
address minter,
address[] memory pausers
) public initializer {
StandaloneERC20.initialize("MyToken", "MTK", uint8(18), [minter], pausers);
}
但是由于此编译器错误,这似乎与我的预期完全不同:
Member "initialize" not found or not visible after argument-dependent lookup in type(contract StandaloneERC20).
StandaloneERC20.initialize("MyToken", "MT", uint8(18), [minter], pausers);
^------------------------^
如何将单个参数作为数组传递?
答案 0 :(得分:2)
这是一种使用单个参数声明数组的方法
2.0
以下是contract MyToken is StandaloneERC20 {
function initialize(
address owner,
address minter,
address[] memory pausers
) public initializer {
address[] memory minters = new address[](1);
minters[0] = minter;
StandaloneERC20.initialize("MyToken", "MTK", uint8(18), minters, pausers);
}
}
个关于Allocating Memory Arrays的文档