[英]how should I add values to structures with partial arguments in solidity
contract ClusterHeadNode {
struct ClusterNode {
string name;
string[] ordinarynodes;
}
mapping(string => ClusterNode[]) clusternodes;
mapping(string => string[]) headnodes;
function addClusterNode(string memory _basename , string memory _clustername) internal {
clusternodes[_basename].push(ClusterNode(_clustername, null ));
}
function getClusterNodes(string memory _name) public view returns(string[] memory){
return headnodes[_name];
}
}
在上面的代码中,我应该在 clusterNode 结构中添加唯一的名称
在尝试这个时我遇到了一个错误
** contracts/hybridblockchain.sol:19:38: TypeError: Wrong argument count for struct constructor: 1 arguments 给出但预期 2. clusternodes[_basename].push(ClusterNode(_clustername ));
请让我摆脱这种情况,或者他们是否有任何替代解决方案请告知
您的结构包含两种类型: string
和string[]
(字符串数组)。
创建实例时,您将传递ClusterNode(_clustername, null )
。 但是null
在 Solidity 中不是有效值,编译器会忽略它(不是因为它无效,而是因为它为空)。
解决方案:传递一个空数组
我根据您的原始代码制作了一个传递空数组的缩小示例:
pragma solidity ^0.8.0;
contract ClusterHeadNode {
struct ClusterNode {
string name;
string[] ordinarynodes;
}
mapping(string => ClusterNode[]) clusternodes;
function addClusterNode(string memory _basename, string memory _clustername) external {
string[] memory ordinarynodes; // instanciate empty array
ClusterNode memory clusternode = ClusterNode(_clustername, ordinarynodes); // instanciate the struct, pass the empty array to the struct
clusternodes[_basename].push(clusternode); // push the struct into the array of structs
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.