【问题标题】:Set a default value in constructor在构造函数中设置默认值
【发布时间】:2018-06-14 06:01:06
【问题描述】:

好的。所以我知道这应该很容易做到,我只想在下面设置一个默认值:

uint8 public gasPriceLimit;     //Gas Price Limit

//Constructor
constructor(string _name, string _symbol, uint8 _decimals) public {
  name = _name;
  symbol = _symbol;
  decimals = _decimals;
  uint8 gasPriceLimit = 999;
}

但是,这样做我在编译时收到以下错误:

Type int_const 999 is not implicitly convertible to expected type uint8.

我也尝试在声明本身中设置,但没有运气。

干杯

【问题讨论】:

    标签: solidity


    【解决方案1】:

    好的。对我来说是个谜,但如果我改为使用 uint 而不是 uint8 那么以下工作正常:

    uint public gasPriceLimit = 999;
    

    【讨论】:

    • uint8 有 8 位,所以它可以存储的最大值是 2^8 = 256999 > 256,所以你需要一个更大的整数。 uint16 应该足够了,但是uint256uint 是其别名)就可以了。
    • 但是注意你需要更改gasPriceLimit的第一个声明,然后在构造函数中,你只需要gasPriceLimit = 999。您当前的代码声明了一个隐藏现有变量的新变量,因此它实际上并没有更新状态变量。
    • 啊是的..好点,刚刚在编译器中也看到了:)干杯
    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 2020-12-31
    • 2013-07-30
    • 2016-06-17
    • 2022-01-08
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多