【问题标题】:Ethereum Pet Shop tutorial - ParserError: Expected pragma, import directive or contract/interface/library definition以太坊宠物店教程 - ParserError:预期的编译指示、导入指令或合约/接口/库定义
【发布时间】:2020-07-16 21:36:07
【问题描述】:

我正在尝试按照教程 here 学习如何编写 dapp。我把这个 .sol 代码放在一起(好吧,复制/粘贴...):

pragma solidity ^0.5.0;

contract Adoption {
address[16] public adopters;
}

// Adopting a pet
function adopt(uint petId) public returns (uint) {
  require(petId >= 0 && petId <= 15);

  adopters[petId] = msg.sender;

  return petId;
}

// Retrieving the adopters
function getAdopters() public view returns (address[16] memory) {
  return adopters;
}

并且已经存在另一个文件。当我运行 truffle compile 命令时,我看到以下内容:

ParserError: Expected pragma, import directive or contract/interface/library definition.
function adopt(uint petId) public returns (uint) {
^------^

Compilation failed. See above.
Truffle v5.1.34 (core: 5.1.34)
Node v12.18.2

知道我可能缺少什么吗?

【问题讨论】:

    标签: solidity truffle


    【解决方案1】:

    函数必须在contract 范围内:

    pragma solidity ^0.5.0;
    
    contract Adoption {
      address[16] public adopters;
    
    
      // Adopting a pet
      function adopt(uint petId) public returns (uint) {
        require(petId >= 0 && petId <= 15);
    
        adopters[petId] = msg.sender;
    
        return petId;
      }
    
      // Retrieving the adopters
      function getAdopters() public view returns (address[16] memory) {
        return adopters;
      }
    
    } // <= closing brace
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 2016-02-28
      • 2019-04-07
      • 2018-03-15
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多