DeclarationError: Undeclared identifier - although it's present in ERC721.sol
DeclarationError: Undeclared identifier - although it's present in ERC721.sol
我正在写一份关于 solidity 0.8.3 的合同,我得到了 _setTokenURI()
这个奇怪的错误,尽管方法是 defined in OpenZeppelin 4.X.
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFTB is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
mapping(string => uint8) hashes;
constructor() public ERC721("NFTB", "NFTB") {}
function awardItem(address recipient, string memory hash, string memory metadata) public returns (uint256) {
require(hashes[hash] != 1);
hashes[hash] = 1;
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_setTokenURI(newItemId, metadata);
_mint(recipient, newItemId);
return newItemId;
} }
函数_setTokenURI()
在@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol
(source on GitHub)中定义,但是这个合约不是由你的代码导入的(包括嵌套导入)。这就是函数未声明的原因。
由于 ERC721URIStorage
扩展了 ERC721
,您可以直接从 ERC721URIStorage
扩展您的 NFTB
。
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; // changed import
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFTB is ERC721URIStorage { // changed parent
我正在写一份关于 solidity 0.8.3 的合同,我得到了 _setTokenURI()
这个奇怪的错误,尽管方法是 defined in OpenZeppelin 4.X.
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFTB is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
mapping(string => uint8) hashes;
constructor() public ERC721("NFTB", "NFTB") {}
function awardItem(address recipient, string memory hash, string memory metadata) public returns (uint256) {
require(hashes[hash] != 1);
hashes[hash] = 1;
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_setTokenURI(newItemId, metadata);
_mint(recipient, newItemId);
return newItemId;
} }
函数_setTokenURI()
在@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol
(source on GitHub)中定义,但是这个合约不是由你的代码导入的(包括嵌套导入)。这就是函数未声明的原因。
由于 ERC721URIStorage
扩展了 ERC721
,您可以直接从 ERC721URIStorage
扩展您的 NFTB
。
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; // changed import
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFTB is ERC721URIStorage { // changed parent