首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译Eror:一个合同访问另一个合同的变量

编译Eror:一个合同访问另一个合同的变量
EN

Ethereum用户
提问于 2020-06-24 13:29:09
回答 1查看 47关注 0票数 1

我正在编写两份合同。在"HoneyPotCollect.sol“合同中,我得到了'balance‘变量错误。平衡不是全球契约变量吗?请有人指点我如何改正错误。

//HoneyPot.sol

代码语言:javascript
复制
    pragma solidity ^0.5.0;
      contract HoneyPot {
      mapping (address => uint) public balances;
    
      constructor() payable public {
        put();
      }
    
      function put() payable public {
        balances[msg.sender] = msg.value; // msg.sender here is the address from the sender
      }
    
      function get() public {
         (bool success,) = msg.sender.call.value(balances[msg.sender])("");
          success = false;
          //revert();
        
        balances[msg.sender] = 0;
      }

  function() external {
    revert();
  }
}

//HoneyPotCollect合同

代码语言:javascript
复制
pragma solidity ^0.5.0;

import './HoneyPot.sol';

contract HoneyPotCollect {
  HoneyPot public honeypot;

  constructor(address _honeypot) public {

    honeypot = HoneyPot(_honeypot);
  }

  function kill () public {
    selfdestruct(msg.sender);
  }

  function collect() payable public {
    honeypot.put.value(msg.value)();
    honeypot.get();
  }

  function () external payable  {
    if (honeypot.balance >= msg.value) {
      honeypot.get();
    }
  }
}

以下是我的错误信息:

$ solc HoneyPotCollect.sol HoneyPotCollect.sol:19:9: Error:成员"balance“在依赖于参数的HoneyPot中查找后未找到或不可见。使用“地址(蜜罐).balance”访问此地址成员。if (honeypot.balance >= msg.value) {^

EN

回答 1

Ethereum用户

发布于 2020-06-24 13:47:44

balance不是一个全局契约变量吗?

不是,但它是address类型的成员,所以请更改如下:

代码语言:javascript
复制
honeypot.balance

对此:

代码语言:javascript
复制
address(honeypot).balance

有关更多详细信息,请参阅正式文件

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/84500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档