[英]how to initialize Empty array in stuct [Solidity]
在创建结构时,我正在为结构初始化一个空数组。
pragma solidity ^0.5.1;
contract Board {
//storage
Post[] posts;
//struct
struct Post {
address author;
string title;
string content;
Comment[] comments;
}
struct Comment {
address author;
string comment;
}
//add-post
function addPost(address _author, string memory _title, string memory _content) public {
posts.push(Post(_author, _title, _content, /* HERE IS THE PROBLEM POINT */));
}
}
我想用空数组(类型:注释)初始化注释(结构成员)。 我应该为问题点使用哪个代码?
朗:坚固
谢谢。
老实说,我不知道如何解决这个问题。 我换了一点商店,现在可以用了,也许对你有帮助
Ps 在 0.4.25 版本中您可以返回所有帖子评论,但在 0.5.1 中我认为它不支持作为默认值
pragma solidity ^0.5.1;
contract Board {
//storage
uint256 public postAmount = 0;
mapping(uint256 => Post) public posts;
struct Comment {
address author;
string comment;
}
struct Post {
address author;
string title;
string content;
Comment[] comments;
}
//add-post
function addPost(address _author, string memory _title, string memory _content, string memory _comment) public {
Post storage post = posts[postAmount];
post.author = _author;
post.title = _title;
post.content = _content;
bytes memory tempEmptyString = bytes(_comment);
if (tempEmptyString.length != 0) { // check if comment exists
post.comments.push(Comment({
author: _author,
comment: _comment
}));
postAmount++;
}
}
function getComment(uint256 _postIndex, uint256 _commentIndex) public view returns(string memory) {
Post memory post = posts[_postIndex];
return post.comments[_commentIndex].comment;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.