为啥我的 Solidity 构造函数需要两个未在参数中指定的变量?

Posted

技术标签:

【中文标题】为啥我的 Solidity 构造函数需要两个未在参数中指定的变量?【英文标题】:Why is my solidity constructor expecting two variables that aren't specified in the parameters?为什么我的 Solidity 构造函数需要两个未在参数中指定的变量? 【发布时间】:2021-08-06 23:13:54 【问题描述】:

我正在使用solidity 0.5.0 我的智能合约期望构造函数中有两个变量,而构造函数中没有。率和钱包。这是我的智能合约

contract Crowdsale is Owned
    using SafeMath for uint256;

    // The token being sold
    IERC20 _token;

    // Address where funds are collected
    // How many token units a buyer gets per wei.
    // The rate is the conversion between wei and the smallest and indivisible token unit.
    // So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK
    // 1 wei will give you 1 unit, or 0.001 TOK.
    uint256 rate;

    // Amount of wei raised
    uint256 _weiRaised;
    bool status;  
    address payable wallet;

    /**
     * Event for token purchase logging
     * @param purchaser who paid for the tokens
     * @param beneficiary who got the tokens
     * @param value weis paid for purchase
     * @param amount amount of tokens purchased
     */
    event TokensPurchased(address purchaser, address beneficiary, uint256 value, uint256 amount);

    /**
     * @param rate Number of token units a buyer gets per wei
     * @dev The rate is the conversion between wei and the smallest and indivisible
     * token unit. So, if you are using a rate of 1 with a ERC20Detailed token
     * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
     * @param wallet Address where collected funds will be forwarded to
     * @param token Address of the token being sold
     */
    constructor (IERC20 token) public 
        rate = 100;
        _token = token;
    //    address payable token = Token();
        wallet = 0x64eCe92B79b096c2771131870C6b7EBAE8C2bd7E;
        status = true;
    

这是我不断收到的错误

DocstringParsingError: Documented parameter "rate" not found in the parameter list of the function.
,DocstringParsingError: Documented parameter "wallet" not found in the parameter list of the function.

【问题讨论】:

【参考方案1】:

您的构造函数只接受一个参数 - token。但是您的文档字符串描述了两个附加参数 - ratewallet

 * @param rate Number of token units a buyer gets per wei
 * @param wallet Address where collected funds will be forwarded to

您需要将它们从 Docstring 中指定的参数列表中删除。 IE。删除这些行或将它们标记为常规注释 - 但不要使用 @param 关键字。

【讨论】:

以上是关于为啥我的 Solidity 构造函数需要两个未在参数中指定的变量?的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个 Solidity 函数在断言后返回 0?

五构造函数《2022 solidity8.+ 版本教程到实战》

五构造函数《2022 solidity8.+ 版本教程到实战》

五构造函数《2022 solidity8.+ 版本教程到实战》

默认构造函数,为啥我的类似乎有三个?当编译器将类视为结构时?

舞台上的对象未在构造函数之外初始化