I've followed the lecture on Udemy. But I've got an error

here's my 'Inbox/contracts/Inbox.sol'

pragma solidity ^0.4.17;
contract Inbox {
    string public message;

    function Inbox(string initialMessage) public {
        message = initialMessage;

    function setMessage(string newMessage) public {
        message = newMessage;

and here is a 'Inbox/compile.js' file

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts','Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

console.log(solc.compile(source, 1));

and after I compile this file on terminal

node compile.js

got an error like below

           [ ':6:5: Warning: Defining constructors as functions with the same name 
as the contract is deprecated. Use "constructor(...) { ... }" 
    instead.\n    function Inbox(string initialMessage) public {\n    ^ 
    (Relevant source part starts here and spans across multiple lines).\n' ],

I use mac. One thing I did differently from the lecture was 'npm init' in other folder instead of Inbox and I coppied and pasted all the files created to inbox folder.

  • 1
    mirg's answer is close. But alant is correct, do not put the function keyword in front of the constructor keyword. – Don Crumley Apr 22 '18 at 20:39
  • in the previous version of solidity was correct. It has been changed recently and my post is updated now. – mirg May 2 '18 at 3:38

It should be a warning not an error. Anyway using this syntax

function Inbox(string initialMessage) public {
    message = initialMessage;

is deprecated in the newer version of Solidity. You have to use constructor instead of the contract name

constructor(string initialMessage) public {
    message = initialMessage;

this will compile without warnings

  • 1
    Also, if you're doing this, make sure you change the first line to pragma solidity ^0.4.22;, otherwise if someone tries to compile your contract using an old compiler it won't work as intended. – AnAllergyToAnalogy Apr 18 '18 at 3:09
  • But if you change the constructor function name from theContractName to constructor, then how can you deploy this contract from another contract??? How to fix this code after changing the constructor function name: address addr = new theContractName(..); ??? – Russo Apr 19 '18 at 8:43
  • in the same way you did before, just create a new instance with new – mirg Apr 19 '18 at 8:46
  • Solved here: ethereum.stackexchange.com/questions/46131/… – Russo Apr 19 '18 at 12:02

Just use constructor() should be good.

constructor(string initialMessage) public {
    message = initialMessage;

If you use function constructor(...)... , I think you'll get the following warning:

Warning: This function is named "constructor" but is not the constructor of the contract. If you intend this to be a constructor, use "constructor(...) { ... }" without the "function" keyword to define it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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