ParserError: Expected '{' but got reserved keyword 'override' compiling ERC721 with Truffle and Solidity 0.5

I am new to Solidity and following a tutorial, but cannot get the contract to compile as the instructor does, using the current openzeppelin ERC721 contract.

:computer: Environment

I assume that I am using the latest version of openzeppelin contracts as I used the following command from the Openzeppelin github “$ npm install @openzeppelin/contracts”

I am using Truffle version v5.0.5, Solidity v0.5.0 and node v10.19.0

:memo:Details
I am trying to create a contract for a series of NFTs for digital art I want to put up for sale. I am using the Sublime text editor and have the following code entered into that contract and I am running the command “Truffle compile” on the following contract code.

pragma solidity ^0.5.0;	
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Mecollectibles is ERC721 {
    constructor() ERC721() public {
    }
}

The above code is cut and paste from the actual contract I created following the tutorial. I also tried flattening the code (as alternatively directed in the same tutorial) and referencing the flattened code inside the import statement, but in both cases get the same, following error message.

"Error parsing @openzeppelin/contracts/token/ERC721/ERC721.sol: ParsedContract.sol:106:51: ParserError: Expected '{' but got reserved keyword 'override'
    function balanceOf(address owner) public view override returns (uint256) {"

:1234: Code to reproduce
I’ve created a directory called “nft” and then cd nft into it where I created my ERC721 contract with the contract code above. I also installed the openzeppelin contracts with “$ npm install @openzeppelin/contracts”. I then imported them into my contract with the above code “pragma …” using both the ^0.5.0 and the ^0.6.0 versions of Solidity but I get this same error message.

"Error parsing @openzeppelin/contracts/token/ERC721/ERC721.sol: ParsedContract.sol:106:51: ParserError: Expected ‘{’ but got reserved keyword ‘override’
function balanceOf(address owner) public view override returns (uint256) {
"
when i use the command truffle compile while in the nft directory.

I tried other tutorials and they lead me to this same point.

1 Like

Resolved: The tutorial I was following skips steps. I followed the tutorial on Openzeppelin and no longer get the error message mentioned in this post.

1 Like

Hi @fabianaugustus,

Welcome to the community. :wave:

When you install OpenZeppelin Contacts with npm i @openzeppelin/contracts you get the latest version (currently 3.2 which uses Solidity 0.6). To get a Solidity 0.5 version of OpenZeppelin Contracts (2.x) you can install with npm i @openzeppelin/contracts@2.5.1.

OpenZeppelin Contracts 3.x documentation: https://docs.openzeppelin.com/contracts/3.x/
OpenZeppelin Contracts 2.x documentation: https://docs.openzeppelin.com/contracts/2.x/
OpenZeppelin Learn guides: https://docs.openzeppelin.com/learn/

I also created a draft tutorial for creating an NFT:
[DRAFT] Create an NFT and deploy to a public testnet, using Truffle

Feel free to ask all the questions that you need.