简体   繁体   English

得到 parsererror 我在 remix 上写 inheritance solidity 代码我得到了错误

[英]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.

相关问题 "Solidity 错误 :: ParserError: Expected ',' but got 'Number'" - Solidity Error :: ParserError: Expected ',' but got 'Number' 预期的标识符,但得到“非法”-Solidity - Expected identifier but got 'ILLEGAL' - Solidity 我收到错误 java.text.ParseException: Unparseable date - i got the error java.text.ParseException: Unparseable date 有没有可用的gem可以提取我使用net / http获得的源代码的get和post参数? - any gem available that will pull out the get and post parameters ofsource code that I've got using net/http? 为什么我收到 MySQL 语法错误? - Why am I getting an error on MySQL syntax? 我想从JSON URL获取图像,但是在IOS中获取图像时出现了一些错误 - I want to get image from JSON URL, but I got some error to get the Image In IOS 更改服务器,并获取SyntaxError:JSON.parse:意外的字符parsererror - Changing servers, and getting SyntaxError: JSON.parse: unexpected character parsererror 我在添加固定装置的同时尝试将日期发送到数据库,但出现以下错误。 应用程序崩溃。代码如下 - I am trying to send date to db while adding some fixture but i am getting following error. App is getting crash.Here is the code 我收到解析器错误消息 - I am getting a parser error message 无法将我的数据写入文件? 我看不到错误 - Cannot write my data to a file? I cannot see the error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM