0

First block of code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721URIStorage.sol';
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol';

contract MyNFT is  ERC721URIStorage, Ownable{
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("Snake", "S"){
    }

    function MintNFT(address player, string memory tokenURI) public returns (uint256) {
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);

        return newItemId;
    }

    function NumberOfNFT() public view returns(uint){
        return _tokenIds.current();
    }
}

Second block of code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol";
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721URIStorage.sol';
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol';

contract MyNFT is   ERC721("Snake", "S"),ERC721URIStorage, Ownable{
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor(){
    }

    function MintNFT(address player, string memory tokenURI) public returns (uint256) {
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);

        return newItemId;
    }

    function NumberOfNFT() public view returns(uint){
        return _tokenIds.current();
    }
}

2
  • Can you explain what's the problem?
    – 0xSanson
    Commented Sep 15, 2022 at 17:42
  • Hi SayemAbedin! Welcome to Ethereum Stackexchange! Please, include as much information as possible. For example error message and line are missing.
    – Ismael
    Commented Sep 16, 2022 at 3:24

1 Answer 1

1

In solidity there are function modifiers called virtual and override.

That means, when you mention a function is virtual, you are hinting to the compiler that the function is going to be override in the derived contract. Which is solidity compiler is looking forward to.

In the first block of code, you are importing only erc721URIStorage. While ERC721URIStorage contract importing the ERC721 contract.

So when you try to import the ERC721 contract twice in the second block of code, it is asking you to override virtual functions with same function name.

If you do that, the error is going to be:

enter image description here

If you look at the picture the error says:

TypeError: Derived contract must override function "_burn". Two or more base classes define function with same name and parameter types.

TypeError: Derived contract must override function "tokenURI". Two or more base classes define function with same name and parameter types.

When you are inheriting the erc721 contract twice, you will have two _burn and tokenURI functions. So having two functions with the same name and parameters isn't allowed in any programming language(the compiler throws error) and on top of that they are virtual functions.

Tell me if it helps!

You can read more about inheritence here

2
  • I partially understand now. So in the first block of code am I initializing the ERC20 that is inherited by ERC721URIStorage? Commented Sep 17, 2022 at 7:26
  • ERC721 that is inherited by ERC721URIStorage. But yeah, basically the same if you ignore the standard. Basically, in the second block of code, you are inheriting twice. That is the problem.
    – Ad-h0c
    Commented Sep 17, 2022 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.