【问题标题】:Solidity - Identifier not found or not unique onlyOwner()Solidity - 标识符未找到或不唯一 onlyOwner()
【发布时间】:2021-11-08 18:52:04
【问题描述】:

运行truffle compile 时出现以下松露编译错误:

DeclarationError : Identifier not found or not unique.
   --> project:/contracts/TestCoin.sol:183:56:
    |
183 |     function excludeFromRewards(address account) public onlyOwner() {
    |   

                                                 

我的代币合约中有以下代码。

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract TestCoin is ERC20 {

    constructor(_name, _symbol) {
        _name = name();
        _symbol = symbol();
    }

    .......

    function excludeFromRewards(address account) public onlyOwner() {
        .......
    }

    .......
}

有人知道我在这里缺少什么吗?我似乎无法克服这一点,我正在导入Ownable.sol 文件,所以我对为什么会发生这种情况感到困惑。对于进口的任何东西,我是否需要说使用 Ownable 作为 blah?我会假设它只会继承这些导入。

更新

如果我调用位于 ownable.sol 中的所有者函数,我会得到同样的错误。

if(owner() == msg.sender)

Undeclared identifier.
if(owner() == msg.sender)
   ^^^^^

它似乎无法识别该文件。有什么想法吗?

更新 #2

我想通了,并在下面发布了我的解决方案。谢谢!

【问题讨论】:

  • 你能 eidtar 并添加你所有的代码吗?在我看来,它正在导入其他合约,但“TestCoin”没有继承它们
  • 我已经在我遗漏的结构之上添加了所有内容。构造下面的其余代码只是自定义函数。

标签: compiler-errors compilation solidity


【解决方案1】:

好的,我想通了。我需要将 Ownable 注入合约本身,以便它可用。

contract TestCoin is ERC20, Ownable {
    *********
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2021-07-31
    • 2022-10-23
    • 1970-01-01
    • 2023-03-16
    • 2016-07-25
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多