Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ethereum 成员相等在类型(库断言)中不可用_Ethereum_Truffle - Fatal编程技术网

Ethereum 成员相等在类型(库断言)中不可用

Ethereum 成员相等在类型(库断言)中不可用,ethereum,truffle,Ethereum,Truffle,当我想测试字符串值是否正确时,问题就出现了。数字被正确断言,并且在尝试编译时不会返回错误消息。但是,当我尝试断言字符串时,它返回以下错误消息: Error: Member "equal" is not available in type(library Assert) outside of storage. Assert.equal(token.symbol(), "$", "The symbol of the token should be $"); ^----

当我想测试字符串值是否正确时,问题就出现了。数字被正确断言,并且在尝试编译时不会返回错误消息。但是,当我尝试断言字符串时,它返回以下错误消息:

Error: Member "equal" is not available in type(library Assert) outside of storage.
        Assert.equal(token.symbol(), "$", "The symbol of the token should be $");
        ^----------^
Compiliation failed. See above.
Token.sol

pragma solidity ^0.4.8;

contract Token {
    /* The amount of tokens a person will get for 1 ETH */
    uint256 public exchangeRate;

    /* The name of the token */
    string public name;

    /* The address which controls the token */
    address public owner;

    /* The symbol of the token */
    string public symbol;

    /* The balances of all registered addresses */
    mapping (address => uint256) balances;

    /* Token constructor */
    function Token(uint256 _exchangeRate, string _name, string _symbol) {
        exchangeRate = _exchangeRate;
        name = _name;
        owner = msg.sender;
        symbol = _symbol;
    }

    function getBalance(address account) returns (uint256 balance) {
        return balances[account];
    }
}
pragma solidity ^0.4.8;

// Framework libraries
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";

// Custom libraries and contracts
import "../contracts/Token.sol";

contract TestToken {
    function testExchangeRate() {
        Token token = new Token(500, "Dollar", "$");

        uint256 expected = 500;

        Assert.equal(token.exchangeRate(), expected, "The exchange rate should be 500 tokens for 1 ETH");
    }

    function testSymbol() {
        Token token = new Token(500, "Dollar", "$");

        Assert.equal(token.symbol(), "$", "The symbol of the token should be $");
    }
}
TestToken.sol

pragma solidity ^0.4.8;

contract Token {
    /* The amount of tokens a person will get for 1 ETH */
    uint256 public exchangeRate;

    /* The name of the token */
    string public name;

    /* The address which controls the token */
    address public owner;

    /* The symbol of the token */
    string public symbol;

    /* The balances of all registered addresses */
    mapping (address => uint256) balances;

    /* Token constructor */
    function Token(uint256 _exchangeRate, string _name, string _symbol) {
        exchangeRate = _exchangeRate;
        name = _name;
        owner = msg.sender;
        symbol = _symbol;
    }

    function getBalance(address account) returns (uint256 balance) {
        return balances[account];
    }
}
pragma solidity ^0.4.8;

// Framework libraries
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";

// Custom libraries and contracts
import "../contracts/Token.sol";

contract TestToken {
    function testExchangeRate() {
        Token token = new Token(500, "Dollar", "$");

        uint256 expected = 500;

        Assert.equal(token.exchangeRate(), expected, "The exchange rate should be 500 tokens for 1 ETH");
    }

    function testSymbol() {
        Token token = new Token(500, "Dollar", "$");

        Assert.equal(token.symbol(), "$", "The symbol of the token should be $");
    }
}

为什么会发生这种情况?您如何解决它?

尝试将类型从
string
更改为另一种,例如,
bytes32
。它起作用了


最好的。

到目前为止,solidity不支持在合同之间返回字符串。因为在调用时字符串的长度是未知的。所以它们只支持固定大小的arryas,比如bytes32


您可以使用多个字节32来存储字符串的不同部分。

这可能会有所帮助吗?关于这个话题有什么消息吗?