Receiving error on the override, in my Token Contract?

Hi Thank you very much!! I’m getting a error on the override in my token contract. thinking this might just be a syntax error, but, I don’t see where. it’s reading Expected ‘{’ but got reserved keyword ‘override’ oh, also did I put the backticks in correctly for this post (my first post:) if so, I can put the rest of the contract up here, if that helps to see what’s going on. Cheers, and thank you again!

    function tokenURI(uint256 tokenId) public view override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
    
        return Items[newItemId].url;
    }

Here’s the full contract , this for my nft project, that am doing from Moralis.

pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";

contract Treetoken is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIDs;

    constructor() ERC721("Treetoken", "TREE") {}

    struct Item {
        uint256 id;
        address creator;
        string uri;
    }

    mapping(uint256 => Item) public Items;

    function createItem(string memory uri) public returns (uint256) {
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _safeMint(msg.sender, newItemId);

        Items[newItemId] = Item(newItemId, msg.sender, uri);

        return newItemId;
    }

    function tokenURI(uint256 tokenId) public view override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
    
        return Items[newItemId].url;
    }
}

  • Add a comment like this at the very top of your file to get rid of the license warning
    // SPDX-License-Identifier: MIT

  • remove the ../node_modules/ from the import statement

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

Fix the following typo errors… I haven’t seen the error you spoke of yet… but perhaps fixing these will help

  • _tokenIDs instead of _tokenIds, etc

Take a look at the error messages and line numbers… FYI I was compiling in 0.7.3 using Hardhat not 0.8.0 as the current project I had open was using that version instead but already had the openzepplin imports.

contracts/TreeToken.sol:21:9: DeclarationError: Undeclared identifier. Did you mean "_tokenIDs"?
        _tokenIds.increment();
        ^-------^

contracts/TreeToken.sol:22:29: DeclarationError: Undeclared identifier. Did you mean "_tokenIDs"?
        uint256 newItemId = _tokenIds.current();
                            ^-------^

contracts/TreeToken.sol:33:22: DeclarationError: Undeclared identifier.
        return Items[newItemId].url;
                     ^-------^

contracts/TreeToken.sol:33:16: TypeError: Member "url" not found or not visible after argument-dependent lookup in struct Treetoken.Item storage ref.
        return Items[tokenId].url;
               ^----------------^

Error HH600: Compilation failed

Hope that helps.

1 Like

Right on Brutha man Thank you!! Going to check it right now, and test it out. I totally appreciate it! will reply in a bit and let ya know how it went. Thanks man I’m Geoff by the way, pleasure to meet ya.

hmm, well. changed line 9, switched it lowercase d, was actually supposed to do that there, then all the others matched. Was checkin the ERC721 contract, and they all had lower case d’s. So i was like ok, probably should keep it like that. The video from Nick, was using lowercase on this section. But i bet you’re on to something, cause there’s probably a random uppercase D, somewhere, possibly.

going over every line in each file to see if can catch something ha.

@GeoffStengel

Hello, first question - should:

return Items[newItemId].url;

be:

return Items[tokenId].url;

That is the only thing in your code-block that came back as an error for me. I made this quick ERC721 and it compiled okay:

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract MyNft is ERC721 {
    struct x {
        string url;
    }

    mapping (uint256 => x) private Items;

    constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) {
    }

    function tokenURI(uint256 tokenId) public view override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
    
        return Items[tokenId].url;
    }
}

I am running into this same exact issue. did you end up finding the typo?

I found the error. I needed to update the truffle-config.js file to the same solidity version, not just uncomment the line.

version: "0.8.0",    // Fetch exact version from solc-bin (default: truffle's version)