[Ethereum] Defining constructors as functions with the same name as the contract is deprecated. Use “constructor(…) { … }” instead


The warning is from running sample code from Mastering Ethereum, I get the above error and it says

Use "constructor(…) { … }" instead.
function Migrations() public {

Best Answer

This is because of a deprecated standard. In the file, Migrations.sol, replace the line:

function Migrations() public {    


constructor() public {

The line of code shown just above is a constructor, it runs on a contract's deployment, and it is used(as in the file in context) to save the contract owner's address(msg.sender varies based on who interacts with the contract).

This replaces the existing implementation of using a function with the same name as the contract to act as a constructor with a seperate constructor function.

Related Topic