如何解决我应该如何将值添加到具有部分参数的结构中
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:类型错误:结构构造函数的参数计数错误:给出了 1 个参数,但预期为 2.clusternodes[_basename].push(ClusterNode(_clustername));
请让我摆脱这种情况,或者他们是否有任何替代解决方案,请告知
解决方法
您的结构包含两种类型:string
和 string[]
(字符串数组)。
当您创建实例时,您传递的是 ClusterNode(_clustername,null )
。但是 null
在 Solidity 中不是有效值,编译器会忽略它(不是因为它无效,而是因为它为 null)。
解决方法:传递一个空数组
我根据您的原始代码制作了一个传递空数组的缩小示例:
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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。