[英]getting parsererror I write inheritance solidity code on remix i got error
from solidity: contracts/inheritance add two contract.sol:32:1: ParserError: Function, variable, struct or modifier declaration expected.从solidity:contracts/inheritance 添加两个contract.sol:32:1:ParserError:Function,变量,结构或修饰符声明预期。 contract Mycontract is Ownable{ ^------^
合同 Mycontract 是 Ownable{ ^-----^
//code pragma solidity ^0.6.0; //代码杂注solidity ^0.6.0;
contract Ownable{合约拥有{
address public owner;地址公共所有者;
constructor( string memory _secret)public{
owner = msg.sender;
}
modifier addcontract(string memory _secret){
require(owner == msg.sender, "only owner can accesss");
_;
}
} }
contract secretvault { string secret;合同秘密保险库{字符串秘密;
constructor() public{
secret = _secret;
}
function getsecret() public view returns(string memory){
return secret;
} }
contract Mycontract is Ownable{ //error is in this line contract Mycontract is Ownable{ //错误在这一行
address vault;
constructor(string memory _secret) public{
secretvault _Vault = new Vault(_secret);
secretvault = address(_Vault);
super;
}
function getsecret() public view addcontract returns(string memory){
secretvault _Vault = Vault(secretvault);
return _Vault.getsecret();
}
} }
If the contract you are inheriting from expects arguments to its constructor, then you should pass those arguments like Ownable("secret value")
in one of two places:如果您继承的合同期望 arguments 到它的构造函数,那么您应该在两个地方之一传递那些 arguments 像
Ownable("secret value")
:
On the line here we declare the inheritance:在这里,我们声明 inheritance:
contract MyContract is Ownable("secret value") {
address vault;
constructor(string memory _secret) public {
secretvault _Vault = new Vault(_secret);
secretvault = address(_Vault);
super;
}
function getsecret() public view addcontract returns(string memory){
secretvault _Vault = Vault(secretvault);
return _Vault.getsecret();
}
}
Or on the child constructor:或者在子构造函数上:
contract MyContract is Ownable {
address vault;
constructor(string memory _secret) Ownable("secret value") public {
secretvault _Vault = new Vault(_secret);
secretvault = address(_Vault);
super;
}
function getsecret() public view addcontract returns(string memory){
secretvault _Vault = Vault(secretvault);
return _Vault.getsecret();
}
}
By the way, I'm not sure what you are trying to do or if you are simply playout around with some concepts, but there is no such thing as "private" or "secret" data in smart contracts and blockchain.顺便说一句,我不确定您要做什么,或者您是否只是在玩一些概念,但是智能合约和区块链中没有“私人”或“秘密”数据之类的东西。 All the data in a smart contract can be accessed directly from the blockchain, using something like
web.eth.getStorageAt(contractAddress, storageIndex)
.智能合约中的所有数据都可以直接从区块链访问,使用
web.eth.getStorageAt(contractAddress, storageIndex)
类的东西。
Also, I recommend you to ask these Ethereum/Solidity related question in the dedicated community: https://ethereum.stackexchange.com/另外,我建议您在专门的社区中询问这些与 Ethereum/Solidity 相关的问题: https://ethereum.stackexchange.com/
By the way, trying to guess what you are trying to do, I refactored your code to look like this:顺便说一句,试图猜测您要做什么,我将您的代码重构为如下所示:
//code
pragma solidity ^0.6.0;
contract Ownable {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(owner == msg.sender, "Only owner can accesss");
_;
}
}
contract Vault {
// There is nothing actually private in a smart contract or blockchain.
string private secret;
constructor(string memory _secret) public {
secret = _secret;
}
function getSecret() public view returns(string memory){
return secret;
}
}
contract MyContract is Ownable {
Vault vault;
constructor(string memory _secret) public {
vault = new Vault(_secret);
}
function getSecret() public view onlyOwner returns(string memory){
return vault.getSecret();
}
}
In secretvault
smart contract declaration, you must close curly brackets at the end (after getsecret()
function) in this way:在
secretvault
智能合约声明中,您必须以这种方式在末尾(在getsecret()
函数之后)关闭大括号:
contract secretvault {
string secret;
constructor() public {
secret = _secret;
}
function getsecret() public view returns(string memory){
return secret;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.