首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:在合同游戏中,在依赖于参数的查找之后找不到或看不到成员“所有者”

TypeError:在合同游戏中,在依赖于参数的查找之后找不到或看不到成员“所有者”
EN

Stack Overflow用户
提问于 2022-08-11 07:49:04
回答 1查看 119关注 0票数 1

我试图使用“松露测试”测试此合同,但它显示了以下错误:"TypeError:成员“所有者”未找到“,或在”owner = gaming.owner();“中的依赖于参数的查找后不可见。

Gaming.sol

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

contract Gaming {
    /* Our Online gaming contract */
    address owner;
    bool public online;

    struct Player {
        uint wins;
        uint losses;
    }

    mapping (address => Player) public players;

    constructor() public payable {
        owner = msg.sender;
        online = true;
    }

    modifier isOwner() {
        require(msg.sender == owner, "Only owner can call this function");
        _;
    }

}

TestGaming.sol

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

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Gaming.sol";

contract TestGaming {
    uint public initialBalance = 10 ether;
    Gaming gaming;
    address owner;

    function beforeAll() public {
        gaming = Gaming(DeployedAddresses.Gaming());
        owner = gaming.owner();

    }

    function testWithdrawFunds() public {
        uint ownerBalanceBefore = owner.balance;
        gaming.withdrawFunds();
        uint ownerBalanceAfter = owner.balance;

        Assert.equal (initialBalance, ownerBalanceAfter - ownerBalanceBefore, "The owner's balance should have increased by 10 ether");
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-27 19:24:42

在稳固性中,默认可见性是internal。对于此代码

代码语言:javascript
复制
address owner;

它的能见度是内在的。Solidity为公共变量分配getter。

代码语言:javascript
复制
address public owner;

您可以很容易地在混合上测试:

代码语言:javascript
复制
contract Test {        
    uint public firstVar=10;
    uint secondVar=50;
  }

如果您将其部署在混合上,您将看到getter用于firstVar,而不是secondVar

或者您可以为所有者编写getter函数:

代码语言:javascript
复制
 function returnOwner() public view returns (address){
        return owner
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73317097

复制
相关文章

相似问题

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